2017-06-13 59 views
-1

我試圖設置android:layout_weight="1",但它什麼也沒做。Android layout_weight不起作用

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:background="#fbd0d5" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.raza.helloworld.MainActivity" 
    tools:showIn="@layout/activity_main" 
    android:weightSum="1"> 


    <TextView 

     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Minnions" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Happy" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Birthday" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 
</LinearLayout> 

這段代碼有什麼問題?

+0

在您的線性佈局(根視圖)代替機器人:weightSum = 「3」 –

回答

0

移除機器人:weightSum =從根佈局 「1」

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:background="#fbd0d5" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    > 


    <TextView 

     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Minnions" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Happy" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Birthday" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 
</LinearLayout> 
+0

AW總和感謝您的幫助我正在學習Android開發它可以幫助 –

+0

@RazaKhan你」歡迎 – user2025187

0

更改weightsum至3在母體線性佈局

android:weightSum="3" 
0

只是刪除weigtsumshowIn

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:orientation="vertical" 
    android:layout_height="match_parent" 
    android:background="#fbd0d5" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 


    <TextView 

     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Minnions" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Happy" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 

    <TextView 

     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     android:background="#fef65b" 
     android:text="Birthday" 
     android:textColor="#f44242" 
     android:textSize="60sp" 
     android:textStyle="bold" /> 
</LinearLayout> 
0

您需要將LinearLayout的權重更改爲等於3.

android:weightSum="3" 

This other thread povides更詳細一點對體重總和,但它的要點是:

子元素layout_weight應該等於給母公司weight_sum

+0

它工作時,我從根佈局刪除\t android:weightSum =「1」 –

0

您所有的觀點都有相同重量!所以他們的身高都不會改變。改變weightSum等於其子女的體重總和,併爲子視圖設置不同的權重。它會改變。

像這樣

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#fbd0d5" 
android:orientation="vertical" 
android:weightSum="10" 
app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="4" 
    android:background="#fef65b" 
    android:text="Minnions" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="3" 
    android:background="#fef65b" 
    android:text="Happy" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="0dp" 
    android:layout_weight="3" 
    android:background="#fef65b" 
    android:text="Birthday" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 
</LinearLayout> 
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 


<TextView 
    android:id="@+id/textView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:layout_weight="1" 
    android:background="#fef65b" 
    android:gravity="center" 
    android:text="Hai" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:layout_weight="1" 
    android:background="#fef65b" 
    android:gravity="center" 
    android:text="Hello" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="10dp" 
    android:layout_weight="1" 
    android:background="#fef65b" 
    android:gravity="center" 
    android:text="How r u" 
    android:textColor="#f44242" 
    android:textSize="60sp" 
    android:textStyle="bold" /> 
</LinearLayout>