gpt4 book ai didi

Android RelativeLayout 和 layout_width ="match_parent"的 child

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:05:13 26 4
gpt4 key购买 nike

当我在同一“行”有两个 View,一个 width="fill_parent"时,Android 如何计算 View 的大小?

例子:

<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<EditText
android:id="@+id/EditText01"
android:hint="Enter some text..."
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_toLeftOf="@+id/Button01"
android:layout_height="wrap_content"></EditText>
<Button
android:id="@+id/Button01"
android:text="Press Here!"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"></Button>
</RelativeLayout>

此代码为 EditText 提供所有可用空间,并在其右侧显示按钮。但是随着这个改变,EditText 填充了所有的宽度并且按钮在屏幕之外:

<RelativeLayout  
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<EditText
android:id="@+id/EditText01"
android:hint="Enter some text..."
android:layout_alignParentLeft="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"></EditText>
<Button
android:id="@+id/Button01"
android:text="Press Here!"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/EditText01"
android:layout_height="wrap_content"></Button>
</RelativeLayout

最佳答案

android:layout_toLeftOf="@+id/Button01"将强制 EditText 的右边界与 Button 的左侧对齐, 因此 Android 会忽略 match_parent width 强制宽度只填充从左父侧到按钮的右侧。

android:layout_toRightOf="@+id/EditText01"将强制 Button 的左边界与 EditText 的右侧对齐, 但由于 EditTextmatch_parent右侧的宽度与父 View 的右侧对齐,Android 将强制按钮离开屏幕。

关于Android RelativeLayout 和 layout_width ="match_parent"的 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16545633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com