简体   繁体   English

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

[英]Android linearLayout children not expanding to fill parent

I have the following as a layout of mine:我有以下作为我的布局:

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

This makes my edittext and ratingbar be pushed up to the top and both of the heights are just 'wrap_content'.这使我的 edittext 和 ratingbar 被推到顶部,并且两个高度都只是“wrap_content”。 I have tried with RelativeLayouts as well and all that happens is the EditText takes up the entire screen and pushes the ratingbar off the screen.我也尝试过使用RelativeLayouts,发生的一切都是EditText 占据整个屏幕并将评级栏推离屏幕。

Why are the layout weights not working as expected?为什么布局权重没有按预期工作? If edittext weight is 3 and the ratingbar is 1 I would assume the edittext would take 75% of the screen, and so forth?如果 edittext 权重为 3 并且 ratingbar 为 1 我会假设 edittext 将占据屏幕的 75%,依此类推?

The background is a 9patch image so it should have no problem expanding either.背景是 9patch 图像,因此扩展也不会有问题。

Thank you谢谢

I believe you want something like this我相信你想要这样的东西

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

Change LinearLayout's更改 LinearLayout 的

android:layout_height="wrap_content"

to

android:layout_height="match_parent".

Also, instead of setting "0dp" to EditText and RatingBar set it to match_parent .此外,不是将“0dp”设置为 EditText 和 RatingBar 将其设置为match_parent

[Update] [更新]

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

use Relative Layout to center object:使用相对布局来居中对象:

<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