Android 在屏幕下方 1/4 处放置一个按钮
Android place a button 1/4 of the way down the screen
假设黄线是中心线,黑线是黄色和顶部之间的中点。红色框代表我想放置按钮的位置。我不确定如何定义这些位置或使按钮以它们为中心而不是在它们上方或下方。我宁愿在 xml 中进行,而不是在 java 中进行,但如果需要,我会这样做。可能和android:weight有关。谢谢
类似的东西(省略冗长的属性):
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button android:centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button android:centerInParent="true" />
</RelativeLayout>
</LinearLayout>
编辑
因为显然没有人会这样做:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button ... />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
<Button ... />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
给按钮添加必要的属性,就可以了。
我已经测试了这个 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
假设黄线是中心线,黑线是黄色和顶部之间的中点。红色框代表我想放置按钮的位置。我不确定如何定义这些位置或使按钮以它们为中心而不是在它们上方或下方。我宁愿在 xml 中进行,而不是在 java 中进行,但如果需要,我会这样做。可能和android:weight有关。谢谢
类似的东西(省略冗长的属性):
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button android:centerInParent="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<Button android:centerInParent="true" />
</RelativeLayout>
</LinearLayout>
编辑
因为显然没有人会这样做:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button ... />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2" />
<Button ... />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>
给按钮添加必要的属性,就可以了。
我已经测试了这个 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>