2017-06-13 76 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> 
相关问题