Android 获取布局的第二季度';身高
我在设计我的应用程序时遇到了一些困难。我的屏幕应该包含工具栏和一些编辑字段。这些字段应位于工具栏下屏幕的上部。最后,它应该是这样的(对不起,图像的质量): 为此,我决定将屏幕高度分成四个相等的部分,并在第二季度设置编辑字段的姿势。我尝试使用以下代码来实现这一点,但没有帮助:所有较小的相对布局占据了所有屏幕。 我还尝试:Android 获取布局的第二季度';身高,android,android-layout,Android,Android Layout,我在设计我的应用程序时遇到了一些困难。我的屏幕应该包含工具栏和一些编辑字段。这些字段应位于工具栏下屏幕的上部。最后,它应该是这样的(对不起,图像的质量): 为此,我决定将屏幕高度分成四个相等的部分,并在第二季度设置编辑字段的姿势。我尝试使用以下代码来实现这一点,但没有帮助:所有较小的相对布局占据了所有屏幕。 我还尝试: 用线性布局替换父相对布局的步骤 将“填充父项”替换为“包装内容” 在这些动作之后,较小的布局就消失了 <RelativeLayout xmlns:android="h
- 用线性布局替换父相对布局的步骤
- 将“填充父项”替换为“包装内容”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false" >
<include android:id = "@+id/toolbarLogo" layout="@layout/toolbar_logo"></include>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.25"
>
<EditText
.../>
<EditText
...
/>
<Button
.../>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="0.5"
/>
</RelativeLayout>
您能帮我解决这个问题吗?将根布局更改为线性布局,并确保方向设置为垂直。将子对象的布局宽度更改为1。有关示例,请参见下面的代码:
<?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_height="match_parent"
android:layout_weight="1"
android:background="#f00"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0f0"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00f"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#f0f"/>
</LinearLayout>
试试看。。设置
android:layout_weight="1"
对于每个线性布局,以及
设置
对于父线性布局和
就像这个例子
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="4">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0ff"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ff2"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#01a"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#101"/>
</LinearLayout>
</RelativeLayout>
您可以使用google library
PercentRelativeLayout
使用此库
您可以通过Procentage设置视图的宽度、高度和边距,这非常好,因为在所有屏幕上,它们看起来都一样,当然编码也不难。例如:
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
app:layout_widthPercent="50%"
app:layout_heightPercent="50%"
app:layout_marginTopPercent="25%"
app:layout_marginLeftPercent="25%"/>
</android.support.percent.PercentRelativeLayout>
谷歌官方文档
对于您的案例,应该如下所示:
<RelativeLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_heightPercent="10%"
/>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/firstLayout"
android:orientation="vertical"
app:layout_heightPercent="40%">
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<Button
android:layout_width="200dp"
android:layout_height="60dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/secondLayout"
app:layout_heightPercent="30%"
/>
</android.support.percent.PercentRelativeLayout>
希望这有帮助
dependencies {
compile 'com.android.support:percent:23.2.0'
}
<RelativeLayout
android:id="@+id/firstLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_heightPercent="10%"
/>
<LinearLayout
android:id="@+id/secondLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/firstLayout"
android:orientation="vertical"
app:layout_heightPercent="40%">
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<EditText
android:layout_width="200dp"
android:layout_height="60dp"/>
<Button
android:layout_width="200dp"
android:layout_height="60dp"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/secondLayout"
app:layout_heightPercent="30%"
/>
</android.support.percent.PercentRelativeLayout>