Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在布局中正确定位按钮_Android_Android Layout - Fatal编程技术网

Android:在布局中正确定位按钮

Android:在布局中正确定位按钮,android,android-layout,Android,Android Layout,我的布局如下: <LinearLayout //container, should adjust height based on CONTENT view height android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"

我的布局如下:

<LinearLayout //container, should adjust height based on CONTENT view height
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:clickable="false"
        android:padding="20dp">
     <RelativeLayout //this is the CONTENT view height
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="5">....</RelativeLayout>
...
    <RelativeLayout //this is the button layout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2">

          <Button android:layout_width="40sp" android:layout_height="40sp"/>
          <Button android:layout_width="40sp" android:layout_height="40sp"/>
    </RelativeLayout>

</LinearLayout>


....
...
我希望调整容器的高度(
LinearLayout
)以包含
RelativeLayout
中的所有视图(如左图所示,我们称之为容器

然后,
RelativeLayout
中有两个按钮(显示在右侧)。我需要将它们对应地对齐在
RelativeLayot
的上下边框上。真正重要的是,按钮容器的高度应该与容器的高度相同(应该对应)

问题是,如果我尝试为按钮使用
android:layout\u alignParentBottom=“true”
android:layout\u alignParentTop=“true”
属性,它们将拉伸容器高度,并占据整个屏幕高度

那么,我应该用什么魔法来变戏法呢?:)

用这个

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:baselineAligned="false"
        android:clickable="false"
        android:padding="20dp">
     <RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="5"></RelativeLayout>

    <RelativeLayout 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_weight="2">

          <Button android:layout_width="wrap_content" 
              android:layout_height="wrap_content"
              android:layout_alignParentTop="true"/>
          <Button android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:layout_alignParentBottom="true"/>
    </RelativeLayout>

</LinearLayout>

尝试将右侧相对布局的顶部和底部与左侧对齐。 试着这样做:

<RelativeLayout //container, should adjust height based on CONTENT view height
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:clickable="false"
        android:padding="20dp">
     <RelativeLayout //this is the CONTENT view height
            android:id="@+id/contentRL"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="5"
            android:layout_alignParentLeft="true">....</RelativeLayout>
...
    <RelativeLayout //this is the button layout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_alignTop="@id/contentRL"
            android:layout_alignBottom="@id/contentRL"
            android:layout_alignParentRight="true">

          <Button android:layout_width="40sp" 
            android:layout_height="40sp"
            android:layout_alignParentTop="true"/>
          <Button android:layout_width="40sp"
            android:layout_height="40sp"
            android:layout_alignParentBottom="true"/>
</RelativeLayout>

....
...

好的,根据Damien R提供的提示。我通过以下操作成功完成了任务:

  • 使用
    RelativeLayout
    作为带有参数的根
    layout\u width=“wrap\u content”
    layout\u height=“wrap\u content”
  • 使用
    LinearLayout
    作为容器
    RelativeLayout
    s的“包装器”。这是因为我需要使用
    layout\u weight
    属性来布置这些容器
  • RelativeLayout
    布局高度
    应为
    fill\u parent
    。无需在
    RelativeLayout
    属性中使用
    android:layout\u alignBottom=“@id/…”
    android:layout\u alignBottom=“@id/…”
    。只有当
    RelativeLayout
    是另一个
    RelativeLayout
    的子视图
    时,这才有效,但事实并非如此,因为我需要使用
    LinearLayout
    的权重
代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:baselineAligned="false"
    android:clickable="false"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/ticketbackground"
        android:id="@+id/ticket_layout"
        >

        <RelativeLayout
            android:id="@+id/contentRL"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="5"
            android:layout_alignParentLeft="true">
        </RelativeLayout>

<!--second column-->
        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="3">
              ...
        </RelativeLayout>
<!--third column with buttons-->
        <RelativeLayout
            android:id="@+id/sdfsdf"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="2">

            <Button...
                android:layout_alignParentTop="true" />
            <Button...
                android:layout_alignParentBottom="true" />
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

...

No,这是不需要的,因为这将拉伸根线性布局hmm,
RelativeLayout
似乎没有这样的(或类似的属性)。还有其他方法吗?我已经更新了我的答案,请再次检查并让我知道。(将主布局更改为“相对”并调整子视图相对位置)。这将不起作用-具有布局权重的视图应具有父线性布局。但是根据你的建议,我找到了做我需要做的事情的方法。