繁体   English   中英

Android linearLayout 子项不会扩展以填充父项

[英]Android linearLayout children not expanding to fill parent

我有以下作为我的布局:

<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"/>

这使我的 edittext 和 ratingbar 被推到顶部,并且两个高度都只是“wrap_content”。 我也尝试过使用RelativeLayouts,发生的一切都是EditText 占据整个屏幕并将评级栏推离屏幕。

为什么布局权重没有按预期工作? 如果 edittext 权重为 3 并且 ratingbar 为 1 我会假设 edittext 将占据屏幕的 75%,依此类推?

背景是 9patch 图像,因此扩展也不会有问题。

谢谢

我相信你想要这样的东西

<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 的

android:layout_height="wrap_content"

android:layout_height="match_parent".

此外,不是将“0dp”设置为 EditText 和 RatingBar 将其设置为match_parent

[更新]

<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"
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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM