android - 在 android XML 中使用相对布局

标签 android xml button android-relativelayout

我正在尝试按照 http://i.stack.imgur.com/aPoeU.png 的内容在 XML 中创建布局但我对如何以这种格式放置按钮感到困惑。我已经创建了按钮并尝试了不同的东西,如 alignParentBottom、alignParentRight 等,但我似乎无法按照我想要的方式获得它。有人可以帮帮我吗?

<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"
tools:context=".MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1"
    android:text="Button" />

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button2"
    android:text="Button" />

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button3"
    android:text="Button" />

最佳答案

我们可以通过weight_sum来管理全屏全分辨率

将下面的代码粘贴到您的 xml 中

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:background="#000"
    android:weightSum="1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

     android:background="#bebebe"
        android:layout_weight="0.30"
        android:text="Button1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:layout_weight="0.20"
        android:background="#bebebe"
        android:text="Button2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:layout_weight="0.20"
        android:background="#bebebe"
        android:text="Button3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="2dp"
        android:layout_weight="0.30"
        android:background="#bebebe"
        android:text="Button4" />

</LinearLayout>

enter image description here

关于android - 在 android XML 中使用相对布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27387749/

相关文章:

java - Android等待异步任务完成执行特定方法

Java 对 PMML 的支持

c# - 在类中使用数组进行 XML 反序列化

android - 如何在 Android 中强制蓝牙 LE "Just Works"配对

java - getStringArrayExtra() 返回 null

java - 具有惯性和边缘阻力/回弹的平滑滚动

xml - 有没有办法在 Scala 中获取 "reverse"XPath?

ios - 如何以编程方式增加按钮中的字体大小(swift 3)

javascript - 我们可以通过javascript禁用浏览器的按钮(后退/前进/刷新)

android - 尝试隐藏操作栏项目时出现 NullPointerException