我有三个按钮,大小有点大,现在我需要将第 4 个按钮放在第 2 个按钮下方,并将第 4 个按钮的中心与第 2 个按钮的中心对齐。
我这样做有点麻烦,有什么想法吗?
!---!---!---! !!!! !---!---!---! !--------! !! !--------!
我一直在玩重力和对齐中心和东西,但仍然找不到我需要的东西。这件事不让我在没有几行的情况下发帖。我想知道添加这条线是否会有所作为?
我有三个按钮,大小有点大,现在我需要将第 4 个按钮放在第 2 个按钮下方,并将第 4 个按钮的中心与第 2 个按钮的中心对齐。
我这样做有点麻烦,有什么想法吗?
!---!---!---! !!!! !---!---!---! !--------! !! !--------!
我一直在玩重力和对齐中心和东西,但仍然找不到我需要的东西。这件事不让我在没有几行的情况下发帖。我想知道添加这条线是否会有所作为?
发布的大多数其他解决方案仅依赖于将第四个按钮在屏幕上居中,但如果第二个按钮 未居中,则不解决如何实现这一点。
ConstraintLayout
是用作父级来完成此任务的完美布局。让您做的最重要的事情ConstraintLayout
是基于另一个视图将一个视图居中,而不管“锚”视图的位置如何。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/two"
android:text="ONE"/>
<Button
android:id="@+id/two"
android:layout_width="60dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/one"
app:layout_constraintRight_toLeftOf="@+id/three"
android:text="TWO"/>
<Button
android:id="@+id/three"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/two"
app:layout_constraintRight_toRightOf="parent"
android:text="THREE"/>
<Button
android:id="@+id/four"
android:layout_width="200dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/two"
app:layout_constraintLeft_toLeftOf="@+id/two"
app:layout_constraintRight_toRightOf="@+id/two"
android:text="FOUR"/>
</android.support.constraint.ConstraintLayout>
这是代码
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/top_buttons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/button_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First" />
<Button
android:id="@+id/button_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second" />
<Button
android:id="@+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third" />
</LinearLayout>
<Button
android:id="@+id/button_4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/top_buttons"
android:layout_centerHorizontal="true"
android:text="Forth" />
</RelativeLayout>
请尝试此代码,这将解决您的问题。很高兴能帮助你
<LinearLayout
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"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Button" />
</LinearLayout>
</LinearLayout>
你能试试这样的吗
<LinearLayout
android:weightSum="3">
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
<Button
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:weightSum="3">
<Button
android:layout_gravity="center_horizontal"
android:layout_weight="2"/>
</LinearLayout>
显然填写必要的标签要求
也许你可以试试这个:
创建两个具有相同宽度的 LinearLayout。将前三个按钮放在第一个 LinearLayout 中,将第四个按钮放在第二个中。添加android:gravity="center"
到第二个 LinearLayout。
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:id="@+id/layout1">
<Button
android:id="@+id/button1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="B1" />
<Button
android:id="@+id/button2"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="B2" />
<Button
android:id="@+id/button3"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="B3" />
</LinearLayout>
<LinearLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="@id/layout1"
android:gravity="center"
>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:layout_gravity="center"
/>
</LinearLayout>
</RelativeLayout>