如何使用两个按钮将布局集中在中心和顶部
how to centralize layout in center and top with two buttons
我有一个布局,它有一个框架布局,可以显示图像,在该图像上,我想在屏幕顶部显示两个按钮。在这两个屏幕的背景上有一个透明的水平条,我将其设置为 Relative 或 LinearLayout 的背景。
然后我想在上面放两个按钮,它们之间的距离很好,我希望相对布局的背景在按钮的 right/left 边缘之后结束。在其他情况下,我不希望栏填满整个宽度。我在这里想要什么,我在图片中张贴在这里。查看 link 以更好地了解我想要什么。我已经这样做了,但这不像在某些设备上那样灵活它看起来很奇怪。
并且如果我使用权重和一些权重,那么按钮图像的宽度会拉伸,这看起来很奇怪,因为我的图像在宽度上会拉伸。我有正方形的图像。
这是我正在尝试的:
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_image"
android:layout_gravity="center_horizontal">
<ImageButton
android:id="@+id/btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/btn_gallery"
/>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageButton
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/btn_share" />
</LinearLayout>
将 layout_width
设置为 match_parent
。
并将 android:gravity ="center"
添加到两个按钮。
根据您的参考,试试这个以更好地对齐。
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageButton
android:id="@+id/btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageButton
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
使用此代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal"
android:layout_height="60dp" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
我有一个布局,它有一个框架布局,可以显示图像,在该图像上,我想在屏幕顶部显示两个按钮。在这两个屏幕的背景上有一个透明的水平条,我将其设置为 Relative 或 LinearLayout 的背景。
然后我想在上面放两个按钮,它们之间的距离很好,我希望相对布局的背景在按钮的 right/left 边缘之后结束。在其他情况下,我不希望栏填满整个宽度。我在这里想要什么,我在图片中张贴在这里。查看 link 以更好地了解我想要什么。我已经这样做了,但这不像在某些设备上那样灵活它看起来很奇怪。
并且如果我使用权重和一些权重,那么按钮图像的宽度会拉伸,这看起来很奇怪,因为我的图像在宽度上会拉伸。我有正方形的图像。
这是我正在尝试的:
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/transparent_image"
android:layout_gravity="center_horizontal">
<ImageButton
android:id="@+id/btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/btn_gallery"
/>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
<ImageButton
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:background="@android:color/transparent"
android:src="@drawable/btn_share" />
</LinearLayout>
将 layout_width
设置为 match_parent
。
并将 android:gravity ="center"
添加到两个按钮。
根据您的参考,试试这个以更好地对齐。
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:layout_gravity="center_horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageButton
android:id="@+id/btn_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginRight="10dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:gravity="center"
>
<ImageButton
android:id="@+id/btn_share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:background="@android:color/transparent"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
使用此代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="horizontal"
android:layout_height="60dp" >
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.1"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="0.1"
android:text="Button" />
</LinearLayout>
</LinearLayout>