如何在Android中设计以下格式
我在Android中有一个要求,我必须设计四个按钮,如下图所示。我不确定我应该使用哪种布局,以及如何在所有屏幕上调整60-40%的比例。谢谢如何在Android中设计以下格式,android,android-layout,Android,Android Layout,我在Android中有一个要求,我必须设计四个按钮,如下图所示。我不确定我应该使用哪种布局,以及如何在所有屏幕上调整60-40%的比例。谢谢 您可以使用“权重”属性使布局相对。我提供了一个适合你的例子。请注意,本例中的权重和始终为1 这就是结果 使用3个线性布局。第一个线性布局将是垂直的,weigthSum=2,有2个线性布局子项,它们的权重为1,每个子线性布局将是水平的,并有2个按钮。您可能应该在项目中使用百分比支持库 dependencies { compile 'com.
您可以使用“权重”属性使布局相对。我提供了一个适合你的例子。请注意,本例中的权重和始终为1
这就是结果
使用3个线性布局。第一个线性布局将是垂直的,weigthSum=2,有2个线性布局子项,它们的权重为1,每个子线性布局将是水平的,并有2个按钮。您可能应该在项目中使用百分比支持库
dependencies {
compile 'com.android.support:percent:xx.x.x'
}
拖放后,不更改
布局宽度
和布局高度
LinearLayout (Vertical)
LinearLayout (Horizontal) //layout_weight: 0.4
FrameLayout //layout_weight: 0.4
FrameLayout //layout_weight: 0.6
LinearLayout (Horizontal) //layout_weight: 0.6
FrameLayout //layout_weight: 0.6
FrameLayout //layout_weight: 0.4
注意:如果您将match_parent
更改为0dp
,则权重应位于正确的位置,如0.6表示60%,0.4表示40%
代码:
这将对您有所帮助
<?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"
android:background="@android:color/black"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:weightSum="100"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="60"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button2"
android:layout_weight="40"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:weightSum="100"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button3"
android:layout_weight="40"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button4"
android:layout_weight="60"/>
</LinearLayout>
</LinearLayout>
谢谢…您也可以使用线性布局和权重,或者添加您迄今为止尝试过的内容和实际使用的位置请注意权重总和始终为1。事实并非如此。还请注意,嵌套权重对性能有害,这在本例中是指。我会调整的。@Zeke这不是真的。如果高度为0dp,则0.4将占40%,如果高度为0dp,则0.6将占60%,如果高度与父级匹配,则为false。你的答案为真+1
LinearLayout (Vertical)
LinearLayout (Horizontal) //layout_weight: 0.4
FrameLayout //layout_weight: 0.4
FrameLayout //layout_weight: 0.6
LinearLayout (Horizontal) //layout_weight: 0.6
FrameLayout //layout_weight: 0.6
FrameLayout //layout_weight: 0.4
<?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">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="#d00000"></FrameLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="#9be412" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:background="#12b9a3" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.4"
android:background="#b208a7" />
</LinearLayout>
</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"
android:background="@android:color/black"
android:weightSum="100">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="60"
android:weightSum="100"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="60"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button2"
android:layout_weight="40"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:weightSum="100"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button3"
android:layout_weight="40"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="Button4"
android:layout_weight="60"/>
</LinearLayout>
</LinearLayout>