gpt4 book ai didi

Android:2相对布局分为半屏

转载 作者:IT王子 更新时间:2023-10-29 00:02:38 24 4
gpt4 key购买 nike

我尝试了很多次绘制2个相对布局,水平对齐并分成半屏。

enter image description here

我用油漆设计图像以更好地解释我的意思。

有什么建议吗?

最佳答案

另一种无需使用 LinearLayout 即可完成相同任务的方法是在父布局的中间放置一个居中对齐的“shim”,然后将其他元素与它对齐。如果将半角元素的宽度设置为 match_parent,但同时对齐它们的左右两侧,它们最终会缩小以适应。

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.EqualWidthExample" >

<!-- An invisible view aligned to the center of the parent. Allows other
views to be arranged on either side -->
<View
android:id="@+id/centerShim"
android:layout_height="match_parent"
android:layout_width="0dp"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>

<!--Set width to match_parent sets maximum width. alignParentLeft aligns
the left edge of this view with the left edge of its parent. toLeftOf
sets the right edge of this view to align with the left edge of the
given view. The result of all three settings is that this view will
always take up exactly half of the width of its parent, however wide
that may be. -->
<Button
android:id="@+id/btLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/centerShim"
android:text="Left Button" />

<!--Same deal, but on the right -->
<Button
android:id="@+id/btRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/centerShim"
android:layout_below="@+id/tvLeft"
android:text="Right Button" />
</RelativeLayout>

关于Android:2相对布局分为半屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19983335/

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