2010-09-13 1061 views
7

如何設置我的水平寬度LinearLayout的最大寬度?所以如果內容很短(比如一些文本),佈局會縮小,如果內容較長,它不會擴展超過某個最大寬度值。爲LinearLayout設置最大寬度

我更喜歡在XML級別執行此操作。

+0

看到我的答案[這裏](HTTP:/ /stackoverflow.com/a/8619442/236743)使用ViewGroup子類的另一種方法 – Dori 2011-12-23 18:35:42

回答

7

這確實它 - 我需要什麼beyond what was suggested in prior answer是添加另一種佈局(@+id/TheLayout)作爲內容和頂部佈局之間的包裝:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="horizontal" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="100dip" 
android:paddingRight="100dip"> 
<LinearLayout android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/TheLayout" 
    android:layout_gravity="right"> 
    <TextView android:id="@+id/TextView01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="1" 
     android:text="this is my text." 
     android:layout_gravity="center_vertical"> 
    </TextView> 
    <ImageView android:id="@+id/ImageView01" 
     android:layout_height="wrap_content" 
     android:background="@drawable/icon" 
     android:layout_width="fill_parent"> 
    </ImageView> 
</LinearLayout> 
</LinearLayout> 
+0

好!像魅力一樣工作。 – 2017-04-27 11:24:09

0
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    int widthMaxSpec = MeasureSpec.makeMeasureSpec(270 /*pixels*/, MeasureSpec.AT_MOST); 
    super.onMeasure(widthMaxSpec, heightMeasureSpec); 
} 
+0

歡迎來到SO,你更願意解釋爲什麼你的代碼將解決他的問題,謝謝。 – 2016-03-15 09:06:45

相關問題