Android 按钮彼此相邻,但每个按钮都有自己的一面
我在这里找到了我的第一个问题的答案,并成功地将两个按钮设置为相邻:xml文件:Android 按钮彼此相邻,但每个按钮都有自己的一面,android,android-layout,Android,Android Layout,我在这里找到了我的第一个问题的答案,并成功地将两个按钮设置为相邻:xml文件: <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_alignParentTop="fals
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="false"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_below="@id/description"
android:layout_alignParentEnd="false">
<!-- Send it Button -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/confirm_it"
android:textColor="@color/white"
android:textSize="15sp"
android:layout_weight="0"
android:id="@+id/confirm_button"
android:backgroundTint="@color/darkCyan"
android:onClick="sendName"
/>
<!-- Skip it Button -->
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/skip_it"
android:textColor="@color/white"
android:textSize="15sp"
android:id="@+id/skip_button"
android:backgroundTint="@color/darkCyan"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:onClick="skipName"
/>
</LinearLayout>
现在我想做一些其他的事情,我仍然希望两个按钮在水平方向上相邻,但我希望每个按钮(例如,发送按钮)位于页面的最左侧,跳过按钮位于页面的最右侧:
以下是我想要的:
可能吗?怎么做 最简单的答案是将
线性布局
替换为相对布局
,并使用对齐
<RelativeLayout ...>
<Button ...
android:layout_alignParentLeft="true" />
<Button ...
android:layout_alignParentRight="true" />
</RelativeLayout>
最简单的答案是将
线性布局
替换为相对布局
,并使用对齐
<RelativeLayout ...>
<Button ...
android:layout_alignParentLeft="true" />
<Button ...
android:layout_alignParentRight="true" />
</RelativeLayout>
尼克·卡多佐的回答是正确和干净的,我个人会使用这种方法。如果出于某种原因,您绝对需要线性布局,您可以通过以下方式实现您的要求:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP"/>
</LinearLayout>
以下是使用此代码得到的结果:
尼克·卡多佐的回答是正确和干净的,我个人会使用这种方法。如果出于某种原因,您绝对需要线性布局,您可以通过以下方式实现您的要求:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SEND"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SKIP"/>
</LinearLayout>
以下是使用此代码得到的结果:
如果必须使用线性布局。您可以使用weight或weightSum尝试线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
或
如果必须使用线性布局。您可以使用weight或weightSum尝试线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"/>
</LinearLayout>
或
还有更简单的方法。如果在两个按钮之间添加布局,并且布局权重为“1”。这两个按钮根据需要对齐
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button1" />
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/linearLayout2"
android:layout_weight="1" /> <!-- I say there -->
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>
还有更简单的方法。如果在两个按钮之间添加布局,并且布局权重为“1”。这两个按钮根据需要对齐
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button1" />
<LinearLayout
android:orientation="horizontal"
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/linearLayout2"
android:layout_weight="1" /> <!-- I say there -->
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="@+id/button2" />
</LinearLayout>
</LinearLayout>
正是我所需要的。非常感谢。正是我需要的。非常感谢。谢谢克拉夫,我会使用它,我同意,在我的项目中,有时我需要一个线性布局,所以它会很有用。谢谢克拉夫,我会使用它,我同意,在我的项目中,有时我需要一个线性布局,所以它会很有用。