Android 安卓布局不起作用
我正在尝试设置android:layout\u weight=“1”,但它没有任何作用Android 安卓布局不起作用,android,Android,我正在尝试设置android:layout\u weight=“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://schem
<?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>
此代码有什么问题?将父线性布局中的权重和更改为3
android:weightSum="3"
只需删除
weigtsum
和showIn
<?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>
从根布局中删除android: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>
您需要将线性布局的权重和更改为等于3
android:weightSum="3"
提供了有关权重和的更多细节,但其要点是:
子元素
layout\u weight
应等于父元素的weight\u sum
所有视图都具有相同的权重!。因此,它们的高度都不会更改。将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>
aw sum感谢您的帮助我正在学习android开发it帮助当我从根布局中删除android:weightSum=“1”时它会工作在您的线性布局(根视图)中替换android:weightSum=“3”
<?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>