我创建了一个内部带有按钮的相对布局。我正在使用这个xml代码并排创建四个按钮:
<?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"
android:minWidth="25px"
android:minHeight="25px">
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/relativeLayout1">
<Button
android:text="7"
android:layout_alignParentLeft="true"
android:layout_width="170dp"
android:layout_height="75.0dp"
android:id="@+id/button1" />
<Button
android:text="8"
android:layout_marginLeft="165dp"
android:layout_width="150dp"
android:layout_height="75dp"
android:id="@+id/button2"
android:layout_marginRight="119.0dp" />
<Button
android:text="9"
android:layout_marginLeft="310dp"
android:layout_width="150dp"
android:layout_height="75.0dp"
android:id="@+id/button3" />
<Button
android:text="Enter"
android:layout_marginLeft="455dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/button3" />
</RelativeLayout>
</LinearLayout>
问题是我的边距是从相对布局开始的,而不是从我的左侧对象开始的,在本例中是按钮。所以我的问题是:如果我在不同的屏幕尺寸下运行应用程序,我会有任何问题吗?
问题是我的边距是从相对布局开始的,并且 不是来自我的左侧对象,在这种情况下是按钮。
您可以将按钮放在另一个按钮的右侧,边距将在它们之间。
例:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@color/blue" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@id/button"
android:background="@color/accent" />
</RelativeLayout>
请尝试
-使用线性布局,您可以制作完美的图纸,我定义每个按钮"重量"。这样做就是 他们都均匀地共享线宽。如果我们说一到两个,它将在同一行中拥有两倍于其他行的空间
<?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"
android:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/linearLayout1">
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="@+id/button1"
android:layout_weight="1" />
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="@+id/button2"
android:layout_weight="1" />
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="@+id/button3"
android:layout_weight="1" />
<Button
android:text="ENTER"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="@+id/button4"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>