Android布局 - 两个 View 彼此相邻,宽度相等,使用全屏宽度

标签 android android-layout view android-layout-weight

我在 android 中遇到布局问题。我希望两个 View 应该具有相同的宽度,并且它们应该几乎使用屏幕的整个宽度。每个 View 都应包含居中标签。

完成后应该是这样的:

Example

这是我目前所拥有的:

<?xml version="1.0" encoding="utf-8"?>    
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent" android:layout_height="match_parent">

        <View
            android:id="@+id/view1"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="10dp" />

        <View
            android:id="@+id/view2"
            android:layout_width="10dp"
            android:layout_height="90dp"
            android:layout_alignParentTop="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="10dp" />

    </RelativeLayout>

我现在只有宽度的占位符值。

谢谢。

最佳答案

你必须在 xml 中使用 android:layout_weight 属性所以尝试下面的代码希望它能解决你的问题:-

 <LinearLayout
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal">

     <Button
        android:text="Register"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

     <Button
        android:text="Not this time"
        android:id="@+id/cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:padding="10dip"
        android:layout_margin="5dp"
        android:layout_weight="1" />

  </LinearLayout>

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1"

For more info you need to read Linear layout.

关于Android布局 - 两个 View 彼此相邻,宽度相等,使用全屏宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26930360/

相关文章:

android - Wifi 热点组播/广播

Android SafeArgs 生成的操作缺少参数

android - 在不注册 View 的情况下打开上下文菜单

java - 如何将 View 移动到屏幕最近的边缘?

android - 对象可见性

android - 如何使 View 逐渐出现,而不是突然出现

android - ScrollView 底部空白

android - 如何在膨胀 LinearLayout 时添加行边距?

Android:如何构建类似于 Android UI 页面上显示的选项卡

安卓微调器 : center text vertically within spinner