[英]How to achieve this layout in Android (studio)
我叫乔希。 我正在尝试在Android中实现以下布局,但似乎无法拆分内部布局以获取所需的布局。 有人可以帮忙吗。
这是我到目前为止的内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.firstapplication.myapplication.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/imageView" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="@+id/editText" />
</LinearLayout>
</RelativeLayout>
我唯一的问题是,我不知道如何拆分图片中所示的布局。
相应地设置LinearLayout的height参数,以实现两个布局之间的所需比例。
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="**XYZ dp**"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
您可以通过多种方式解决此问题。 我不知道您的布局要求,但您实际上不需要两个LinearLayout
即可解决此问题。 但是,当前您所要做的就是为您的第一个LinearLayout
一个ID,并将高度设置为wrap_content
如下所示:
<LinearLayout
android:id="@+id/LL1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
然后在第二个布局中将位置设置为低于第一个布局,将高度设置为wrap_content
并删除layout_alignParentTop
否则第二个布局将与第一个布局重叠。 因此,您第二次有这样的事情:
<LinearLayout
android:layout_below="@id/LL1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
这会将第二个布局放在第一个布局的下面,但是除非第一个布局始终保持恒定的高度,否则尺寸会有所不同。
如果您知道始终要使用70:30的比例,而不考虑第一个布局高度,则可以改用layout_weights
。 如果您要查找的是我可以添加的内容。
要使比例恒定,请将根布局更改为LinearLayout
而不是RelativeLayout
然后将方向设置为垂直,并将weightSum
为1。
然后将您的第一个LinearLayout
设置为0.7的layout_weight
,将第二个LinearLayout
设置为0.3的layout_weight
。 像这样:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:orientation="vertical">
<LinearLayout
android:id="@+id/LL1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.7"></LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.3"></LinearLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.