Java Android linearLayout子项不展开以填充父项
我的布局如下:Java Android linearLayout子项不展开以填充父项,java,android,android-linearlayout,Java,Android,Android Linearlayout,我的布局如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RatingBar
android:id="@+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
这使得我的编辑文本和分级栏被推到顶部,两个高度都只是“换行内容”。我也尝试过使用RelativeLayouts,结果EditText占据了整个屏幕,并将分级栏从屏幕上推出
为什么布局权重没有按预期工作?如果edittext权重为3,分级栏为1,我会假设edittext将占据屏幕的75%,依此类推
背景是一个9patch图像,因此它在扩展时也应该没有问题
谢谢更改线性布局
android:layout_height="wrap_content"
到
另外,不要将“0dp”设置为编辑文本,而将其设置为“匹配父项”
[更新]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1">
<RatingBar
android:id="@+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:numStars="5"
android:rating="4"
android:stepSize="1" />
</LinearLayout>
</LinearLayout>
我相信你想要这样的东西
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="EditText"
android:ems="10"
android:id="@+id/editText"
android:layout_weight="0.53" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.53">
<RatingBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/ratingBar"/>
</RelativeLayout>
</LinearLayout>
使用相对布局来居中对象:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="@+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>
你想实现什么?我想让编辑文本占据屏幕的上半部分,而分级栏位于下半部分的中心位置。我不知道我以前怎么没有注意到这一点,谢谢!下一个问题是没有显示ratingbar(但视图占据了下半部分)。将权重设置为ratingbar存在一些问题。有关代码,请参阅更新的答案。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MyActivity">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<EditText
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bubble_b"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<RatingBar
android:id="@+id/rating"
style="?android:attr/ratingBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:numStars="5"
android:rating="4"
android:stepSize="0.5"
android:layout_weight="1"/>
</RelativeLayout>