2017-07-27 23 views
2

我試圖在設置3個着色區域1:2:3的比例使用的TextView:爲什麼View中的layout_weight的效果與TextView的行爲不同?

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<TextView 
    android:layout_weight="1" 
    android:background="#FF0000" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
<TextView 
    android:layout_weight="2" 
    android:background="#00FF00" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
<TextView 
    android:layout_weight="3" 
    android:background="#0000FF" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
</LinearLayout> 

具有我的期望結果(屏幕由機器人工作室預覽拍攝):

enter image description here

但當我將TextView更改爲視圖時:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<View 
    android:layout_weight="1" 
    android:background="#FF0000" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
<View 
    android:layout_weight="2" 
    android:background="#00FF00" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
<View 
    android:layout_weight="3" 
    android:background="#0000FF" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    /> 
</LinearLayout> 

佈局更改(android studio預覽屏幕截圖):

enter image description here

是什麼原因?

+0

問題解決了? –

回答

0

你的父母佈局方向是水平的,所以你不能使用layout_width要麼match_parent,WRAP_CONTENT或大於0那麼,爲什麼它給不需要的結果以外的任何值。

欲瞭解更多信息,請閱讀Layout Weight部分。

<View 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="1" 
    android:background="#FF0000" /> 

<View 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="2" 
    android:background="#00FF00" /> 

<View 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:layout_weight="3" 
    android:background="#0000FF" /> 

Actual output by using Wiew

0

它與寬度有關。假設它的水平LinearLayout具有匹配parent作爲LinearLayout的寬度,並且您的每個視圖(View/TextView)都被賦予了權重,則視圖的單個寬度會導致錯誤行爲。

如果您對各項意見寬度是0dp,既會表現在以同樣的方式

1

你一定要試試這樣

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="horizontal" > 

<View 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:background="#FF0000"/> 
<View 
    android:layout_weight="2" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:background="#00FF00" /> 
<View 
    android:layout_weight="3" 
    android:layout_width="0dp" 
    android:layout_height="match_parent" 
    android:background="#0000FF"/> 
</LinearLayout> 
0

替換寬度與0dp。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="#FF0000" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="2" 
     android:background="#00FF00" /> 

    <View 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="3" 
     android:background="#0000FF" /> 
</LinearLayout> 
相關問題