布局按钮对应背景
layout button corresponts to background
在 Android Studio 中,我希望按钮的大小以及按钮之间的距离随显示器的大小而变化。
基本上,我的背景有特殊的按钮位置(我知道这是一种不好的做法),在不同的屏幕上,按钮不适合背景。
我试图通过 ConstraintLayout
来完成此操作,但是 match_constraint
使按钮的大小变大,因为容器和 margins
是静态的。
我试过this answer to a similar question,但效果不佳。
我在 ConstraintLayout
中也没有做到这一点。具有百分比准则的链需要将其子项设置为 wrap_content
。因此按钮的大小将保持不变,例如在横向和纵向模式下。因此我在 LinearLayout 中使用了 "empty" View
s。通过给每个元素、按钮和空视图一个权重,您可以实现按钮大小相对于屏幕宽度的效果。瞧:
<?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="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="Button1" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="Button2" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
</LinearLayout>
在 Android Studio 中,我希望按钮的大小以及按钮之间的距离随显示器的大小而变化。
基本上,我的背景有特殊的按钮位置(我知道这是一种不好的做法),在不同的屏幕上,按钮不适合背景。
我试图通过 ConstraintLayout
来完成此操作,但是 match_constraint
使按钮的大小变大,因为容器和 margins
是静态的。
我试过this answer to a similar question,但效果不佳。
我在 ConstraintLayout
中也没有做到这一点。具有百分比准则的链需要将其子项设置为 wrap_content
。因此按钮的大小将保持不变,例如在横向和纵向模式下。因此我在 LinearLayout 中使用了 "empty" View
s。通过给每个元素、按钮和空视图一个权重,您可以实现按钮大小相对于屏幕宽度的效果。瞧:
<?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="horizontal">
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
<Button
android:id="@+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="Button1" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:text="Button2" />
<View
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".1666" />
</LinearLayout>