2011-03-29 19 views
12

मैं टेक्स्टव्यू बाईं ओर तैरना चाहता हूं (जैसे सीएसएस फ्लोट्स)। मेरे पास इतने सारे टेक्स्टव्यू का कारण है कि मैं चाहता हूं कि मेरे ऐप में हर शब्द क्लिक करने योग्य हो। तो मैं इस परिणाम हैं: enter image description hereएंड्रॉइड: मैं बाईं ओर पाठ दृश्य कैसे बना सकता हूं?

वर्तमान में, मैं निम्नलिखित एक्सएमएल कोड है:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" android:orientation="horizontal" android:baselineAligned="false"> 
    <TextView android:text="@string/nicholasStr" android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_height="wrap_content" android:id="@+id/nicholas" ></TextView> 
    <TextView android:text="@string/wasStr" android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:id="@+id/was" android:layout_height="wrap_content" ></TextView> 
    <TextView android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:id="@+id/dots" android:layout_height="wrap_content" android:text="@string/dots"></TextView> 
    <TextView android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_height="wrap_content" android:text="@string/older" android:id="@+id/older"></TextView> 
    <TextView android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_height="wrap_content" android:text="@string/older" android:id="@+id/older"></TextView> 
    <TextView android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_height="wrap_content" android:text="@string/older" android:id="@+id/older"></TextView> 
    <TextView android:text="@string/older" android:layout_width="wrap_content" android:textSize="18sp" android:textStyle="bold" android:layout_height="wrap_content" android:id="@+id/older"></TextView> 
</LinearLayout> 

लेकिन परिणाम इस प्रकार है:

enter image description here

मैं एंड्रॉयड के लिए नया हूँ देव, अग्रिम में बहुत धन्यवाद। :)

+0

'का उपयोग करके android का प्रयास करें: अभिविन्यास =" ऊर्ध्वाधर "'। –

+0

'रिलेवेटिवआउट' के साथ आज़माएं और यह आलेख भी देखें [लिंक] http://developer.android.com/resources/articles/layout-tricks-efficiency.html – ASMaitre

+1

यदि आप उन्हें xml के बजाय जावा में गतिशील रूप से बनाते हैं। आप स्क्रीन के किनारे से कितना दूर है यह देखने के लिए कि आप अगली पंक्ति पर कूदने की आवश्यकता है, तो आप आखिरी के दाएं किनारे को देख सकते हैं। – FoamyGuy

उत्तर

19

आपको वास्तव में जो चाहिए वह एक पंक्ति लेआउट है जो पूरे विचारों को लपेटता है। दुर्भाग्यवश एंड्रॉइड में एक नहीं है। हालांकि आपके लिए भाग्यशाली, मैं करता हूँ! ;)

यह एक कस्टम वर्ग है, लेकिन आप ठीक रूप में लंबे समय एक्सएमएल में उपयोग कर सकते हैं आप पूरी तरह से यह चुने जाते हैं, और रेस के अंतर्गत निम्नलिखित attrs.xml फ़ाइल को शामिल के रूप में/महत्व देता

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="RowLayout"> 
     <attr name="android:verticalSpacing" /> 
     <attr name="android:horizontalSpacing" /> 
    </declare-styleable> 
</resources> 

यहाँ है स्रोत:

package com.example.widget; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.List; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 

import com.example.widget.R; 

public class RowLayout extends ViewGroup { 
    public static final int DEFAULT_HORIZONTAL_SPACING = 5; 
    public static final int DEFAULT_VERTICAL_SPACING = 5; 
    private final int horizontalSpacing; 
    private final int verticalSpacing; 
    private List<RowMeasurement> currentRows = Collections.emptyList(); 

    public RowLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray styledAttributes = context.obtainStyledAttributes(attrs, R.styleable.RowLayout); 
     horizontalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.RowLayout_android_horizontalSpacing, 
       DEFAULT_HORIZONTAL_SPACING); 
     verticalSpacing = styledAttributes.getDimensionPixelSize(R.styleable.RowLayout_android_verticalSpacing, 
       DEFAULT_VERTICAL_SPACING); 
     styledAttributes.recycle(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     final int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     final int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     final int maxInternalWidth = MeasureSpec.getSize(widthMeasureSpec) - getHorizontalPadding(); 
     final int maxInternalHeight = MeasureSpec.getSize(heightMeasureSpec) - getVerticalPadding(); 
     List<RowMeasurement> rows = new ArrayList<RowMeasurement>(); 
     RowMeasurement currentRow = new RowMeasurement(maxInternalWidth, widthMode); 
     rows.add(currentRow); 
     for (View child : getLayoutChildren()) { 
      LayoutParams childLayoutParams = child.getLayoutParams(); 
      int childWidthSpec = createChildMeasureSpec(childLayoutParams.width, maxInternalWidth, widthMode); 
      int childHeightSpec = createChildMeasureSpec(childLayoutParams.height, maxInternalHeight, heightMode); 
      child.measure(childWidthSpec, childHeightSpec); 
      int childWidth = child.getMeasuredWidth(); 
      int childHeight = child.getMeasuredHeight(); 
      if (currentRow.wouldExceedMax(childWidth)) { 
       currentRow = new RowMeasurement(maxInternalWidth, widthMode); 
       rows.add(currentRow); 
      } 
      currentRow.addChildDimensions(childWidth, childHeight); 
     } 

     int longestRowWidth = 0; 
     int totalRowHeight = 0; 
     for (int index = 0; index < rows.size(); index++) { 
      RowMeasurement row = rows.get(index); 
      totalRowHeight += row.getHeight(); 
      if (index < rows.size() - 1) { 
       totalRowHeight += verticalSpacing; 
      } 
      longestRowWidth = Math.max(longestRowWidth, row.getWidth()); 
     } 
     setMeasuredDimension(widthMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(widthMeasureSpec) : longestRowWidth 
       + getHorizontalPadding(), heightMode == MeasureSpec.EXACTLY ? MeasureSpec.getSize(heightMeasureSpec) 
       : totalRowHeight + getVerticalPadding()); 
     currentRows = Collections.unmodifiableList(rows); 
    } 

    private int createChildMeasureSpec(int childLayoutParam, int max, int parentMode) { 
     int spec; 
     if (childLayoutParam == LayoutParams.FILL_PARENT) { 
      spec = MeasureSpec.makeMeasureSpec(max, MeasureSpec.EXACTLY); 
     } else if (childLayoutParam == LayoutParams.WRAP_CONTENT) { 
      spec = MeasureSpec.makeMeasureSpec(max, parentMode == MeasureSpec.UNSPECIFIED ? MeasureSpec.UNSPECIFIED 
        : MeasureSpec.AT_MOST); 
     } else { 
      spec = MeasureSpec.makeMeasureSpec(childLayoutParam, MeasureSpec.EXACTLY); 
     } 
     return spec; 
    } 

    @Override 
    protected void onLayout(boolean changed, int leftPosition, int topPosition, int rightPosition, int bottomPosition) { 
     final int widthOffset = getMeasuredWidth() - getPaddingRight(); 
     int x = getPaddingLeft(); 
     int y = getPaddingTop(); 

     Iterator<RowMeasurement> rowIterator = currentRows.iterator(); 
     RowMeasurement currentRow = rowIterator.next(); 
     for (View child : getLayoutChildren()) { 
      final int childWidth = child.getMeasuredWidth(); 
      final int childHeight = child.getMeasuredHeight(); 
      if (x + childWidth > widthOffset) { 
       x = getPaddingLeft(); 
       y += currentRow.height + verticalSpacing; 
       if (rowIterator.hasNext()) { 
        currentRow = rowIterator.next(); 
       } 
      } 
      child.layout(x, y, x + childWidth, y + childHeight); 
      x += childWidth + horizontalSpacing; 
     } 
    } 

    private List<View> getLayoutChildren() { 
     List<View> children = new ArrayList<View>(); 
     for (int index = 0; index < getChildCount(); index++) { 
      View child = getChildAt(index); 
      if (child.getVisibility() != View.GONE) { 
       children.add(child); 
      } 
     } 
     return children; 
    } 

    protected int getVerticalPadding() { 
     return getPaddingTop() + getPaddingBottom(); 
    } 

    protected int getHorizontalPadding() { 
     return getPaddingLeft() + getPaddingRight(); 
    } 

    private final class RowMeasurement { 
     private final int maxWidth; 
     private final int widthMode; 
     private int width; 
     private int height; 

     public RowMeasurement(int maxWidth, int widthMode) { 
      this.maxWidth = maxWidth; 
      this.widthMode = widthMode; 
     } 

     public int getHeight() { 
      return height; 
     } 

     public int getWidth() { 
      return width; 
     } 

     public boolean wouldExceedMax(int childWidth) { 
      return widthMode == MeasureSpec.UNSPECIFIED ? false : getNewWidth(childWidth) > maxWidth; 
     } 

     public void addChildDimensions(int childWidth, int childHeight) { 
      width = getNewWidth(childWidth); 
      height = Math.max(height, childHeight); 
     } 

     private int getNewWidth(int childWidth) { 
      return width == 0 ? childWidth : width + horizontalSpacing + childWidth; 
     } 
    } 
} 
+0

मेरे जैसे कुछ नौसिखियों को यह जानना अच्छा लगेगा कि उन्हें निम्न पंक्ति की आवश्यकता है: xmlns: ऐप = "http://schemas.android.com/apk/res-auto" रूट लेआउट में। यानी <फ्रेमलेआउट xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto" एंड्रॉइड: layout_width = "match_parent" एंड्रॉइड: layout_height = "wrap_content"> –

+0

ग्रेट उत्तर। एक जादू की तरह काम करता है। यह शानदार होगा अगर आप इसे गिटूब पर रख सकते हैं, शायद एक छोटे से दस्तावेज़ के साथ। मुझे बताएं यदि मैं सहायता कर सकता हूं :) –

0

आप इसे अभिविन्यास = लंबवत के साथ सापेक्ष लेआउट के साथ आसानी से कर सकते हैं।

उदाहरण के लिए

: activity_main.xml फ़ाइल पर

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<Button 
    android:id="@+id/btnButton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button 1"/> 

<Button 
    android:id="@+id/btnButton2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Button 2" 
    android:layout_toRightOf="@+id/btnButton1"/> 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/btnButton3" 
    android:layout_marginTop="94dp" 
    android:text="User :" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
    ....... 

</RelativeLayout> 
0

मैं एक बहुत सरल समाधान पाया विकल्पों में से पंक्तियों के साथ एक मेनू बनाने के लिए:

public class OptionMenu extends LinearLayout { 
private int widthConsumed = 0; 
private LinearLayout currentRow; 

interface OptionRunnable { 
    void onOptionSelected(int option); 
} 

public OptionMenu(Context context) { 
    super(context); 
    setOrientation(VERTICAL); 
} 

public void setOptions(List<Integer> options, OptionRunnable runnable) { 
    getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { 
     @Override 
     public boolean onPreDraw() { 
      getViewTreeObserver().removeOnPreDrawListener(this); 

      Stream.of(options).forEach(option -> { 
       TextView text = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.incl_textviewer_option_button, OptionMenu.this, false); 
       text.setText(option); 
       text.setTag(option); 
       text.setOnClickListener(v -> runnable.onOptionSelected((Integer) v.getTag())); 

       text.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED); 
       getCurrentRow(text.getMeasuredWidth()).addView(text); 
      }); 
      return true; 
     } 
    }); 
} 

private LinearLayout getCurrentRow(int width) { 
    if(currentRow == null) { 
     currentRow = new LinearLayout(getContext()); 
     currentRow.setOrientation(HORIZONTAL); 
     addView(currentRow); 
    } 

    widthConsumed += width; 

    if(widthConsumed < getWidth()) 
     return currentRow; 

    LinearLayout newRow = new LinearLayout(getContext()); 
    newRow.setOrientation(HORIZONTAL); 
    currentRow = newRow; 
    widthConsumed = width; 
    addView(newRow); 

    return newRow; 
} 
} 
संबंधित मुद्दे