我制作了一个相机布局,其中有两个按钮。click
按钮和share
按钮。
问题是我想在同一个视图中对齐这个按钮,但我不能。
这是我的按钮代码
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:layout_gravity="left" />
<Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:layout_gravity="right" />
但我希望click
按钮与share
按钮保持相同的位置...
答案 0 :(得分:2)
您可能在另一个视图的LinearLayout
中使用&#34;相机演示&#34;文本)。但是,如果没有看到完整的布局文件,您可以将它们包装在另一个布局中,如下所示:
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:layout_alignParentLeft="true" />
<Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:layout_alignParentRight="true" />
</RelativeLayout>
答案 1 :(得分:1)
从您的图片中,您的父版块使用的是具有垂直方向的LinearLayout。因此,您添加到其中的每个视图将在垂直方向上逐个放置。
如果要将两个按钮放在同一行中,则必须使用RelativeLayout来包装这两个按钮。并将孩子放在RelativeLayout的右侧和左侧
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Share" />
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Click" />
</RelativeLayout>
答案 2 :(得分:1)
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Click" />
<Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Share" />
</RelativeLayout>
should have one more RelativeLayout as parent of all....
我完成了..
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
>
<Button
android:id="@+id/buttonClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="Click" />
<Button
android:id="@+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Share" />
</RelativeLayout>
</RelativeLayout>
答案 3 :(得分:0)
要更好地了解布局,请按照此Layout
进行操作这里。并根据您的问题更好地使用相对布局
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" //to assign botton to bottom of layout
>