简体   繁体   English

android如何对齐按钮?

[英]android how to align buttons?

i have three buttons so some size, now i need to put the 4th button below the 2nd button and aligned center of the 4th button with the center of the 2nd button. 我有三个按钮,所以有些大小,现在我需要将第4个按钮放在第2个按钮下方,并使第4个按钮的中心与第2个按钮的中心对齐。

i am having a little trouble doing that, any ideas? 我这样做有点麻烦,有什么想法吗?

!---!---!---!
!   !   !   !
!---!---!---!
  !-------!
  !       !
  !-------!

i have been playing around with gravity and align center and stuff but still could not find what i need. 我一直在玩重力并对齐中心和东西,但仍然找不到我需要的东西。 This thing is not letting me post without a few more lines. 这件事不允许我多发布几行。 I wonder if adding this line would make a difference? 我不知道添加这一行是否会有所作为?

Here is the code 这是代码

<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>

Can you try something like this 你可以尝试这样的事情吗

<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>

Obviously fill in the requisite tag requirements 显然填写必需的标签要求

Maybe you can try this: 也许您可以尝试以下方法:

Create two LinearLayouts with the same width. 创建两个具有相同宽度的LinearLayouts。 Put the first three buttons in the first LinearLayout and the 4th in the second one. 将前三个按钮放在第一个LinearLayout中,第四个按钮放在第二个LinearLayout中。 Add android:gravity="center" to the second 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>

Please try this code, this will solve your problem. 请尝试此代码,这将解决您的问题。 happy to help 乐意效劳

 <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>

Most of the other solutions posted rely on simply centering the fourth button in the screen, but don't address how to achieve this if the second button isn't centered. 发布的大多数其他解决方案仅依赖于将屏幕上的第四个按钮居中,但是没有解决如果第二个按钮 居中时如何实现这一点。

ConstraintLayout is the perfect layout to use as the parent to accomplish this. ConstraintLayout是用作父级完成此操作的理想布局。 The most important thing that ConstraintLayout lets you do is center one view based on another, regardless of the position of the "anchor" view. 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM