2015-05-04 123 views
7

有一個TextView,其寬度不應超過其父寬度的1/3。如果其寬度小於父級的1/3,則應具有wrap_content行爲。它的水平兄弟會始終在它旁邊開始。如何設置文本視圖寬度wrap_content但限制爲父寬度的三分之一

試過後,它總是有1/3和2/3的硬切,所以如果text1的空間少於1/3,TextView兩個不會在它旁邊開始。

變化LinearLayoutRelativeLayout,則android:layout_weight="n"不起作用

基本上,需要定義寬度wrap_contentmaxWidth沒過1/3。

有什麼建議嗎?

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_width="0dp" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_weight="2" 
    /> 
</LinearLayout> 

回答

3

我認爲你必須每次更新TextView的,像(我使用按鈕和編輯文本改變TextView的測試此代碼時動態檢查父佈局寬度 - 工作沒有問題):

<LinearLayout 
    android:id="@+id/myLayout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/tvA" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:id="@+id/tvB" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 
</LinearLayout> 

代碼:

  TextView tvA = (TextView) findViewById(R.id.tvA); 
      TextView tvB = (TextView) findViewById(R.id.tvB); 
      LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout); 


      // code to use when the textView is updated 
      // possibly button onClickListener? 
      tvA.measure(0, 0); 
      int textWidth = tvA.getMeasuredWidth(); 
      myLayout.measure(0,0); 
      int layoutWidth = myLayout.getWidth(); 

      if (textWidth > (layoutWidth/3)) { 
       tvA.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f)); 
       tvB.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 2.0f)); 

      } else { 
       tvA.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
       tvB.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
      } 
+0

我不希望它成爲永遠的1/3,但最多到1/3,應當允許小於1/3,兄弟應該在它旁邊不空間。 – lannyf

+0

查看更新的答案... –

+1

謝謝馬克!如果在xml中無法實現,您的答案將是一個。 – lannyf

0

的解決方案是簡單的:您的第二TextView的的寬度必須是 「0dp」 之類的第一個。

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:singleLine="true" 
     android:ellipsize="end" 
    /> 

    <TextView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:ellipsize="end" 
     android:layout_weight="2" 
    /> 
</LinearLayout> 
+0

對不起,我有android:layout_width =「0dp」並且沒有複製錯誤。這是行不通的。 – lannyf

0

如何把它放在父母的1/3寬度的佈局中?

<LinearLayout 
    .. this is main parent 
    android:weightSum="1" > 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="0.33" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"/> 
    </LinearLayout> 
</LinearLayout> 
+0

它實現了父代的三分之一,但沒有「如果父代的寬度小於三分之一,則兄弟應該流動」。 – lannyf

3

基本上,需要定義寬度WRAP_CONTENT和maxWidth沒過1/3。

如果這就是你所需要的,那麼我的建議是廢除Weight方法並動態設置TextView的maxWidth值。

事情是這樣的:

tv.setMaxWidth(((LinearLayout)tv.getParent()).getWidth()/3); 
相關問題