简体   繁体   English

如何在Android中实现此布局(Studio)

[英]How to achieve this layout in Android (studio)

my name is Josh. 我叫乔希。 I am trying to achieve the below layout in Android but I can't seem to split the inner layout to get the layout I want. 我正在尝试在Android中实现以下布局,但似乎无法拆分内部布局以获取所需的布局。 Can someone help please. 有人可以帮忙吗。

This is what I have so far: 这是我到目前为止的内容:

<?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>

The only problem I am having is, I can't figure out how to split the layout as shown in the picture. 我唯一的问题是,我不知道如何拆分图片中所示的布局。

Set the LinearLayout's height parameters accordingly to achieve the desired ratio between two layouts. 相应地设置LinearLayout的height参数,以实现两个布局之间的所需比例。

<LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="**XYZ dp**"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

You can solve this multiple ways. 您可以通过多种方式解决此问题。 I don't know the requirement for your layout but you really don't need two LinearLayout to solve this. 我不知道您的布局要求,但您实际上不需要两个LinearLayout即可解决此问题。 However, with what you have currently all you have to do is give your first LinearLayout an id, and set the height to wrap_content like so: 但是,当前您所要做的就是为您的第一个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">

then in your second layout set the positioning to be below your first one, set the height to wrap_content and remove layout_alignParentTop otherwise your second layout will overlap to your first. 然后在第二个布局中将位置设置为低于第一个布局,将高度设置为wrap_content并删除layout_alignParentTop否则第二个布局将与第一个布局重叠。 So you have something like this for the second: 因此,您第二次有这样的事情:

<LinearLayout
        android:layout_below="@id/LL1"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">

This will put the second layout underneath the first, but the sizing will vary unless the first layout is always a constant height. 这会将第二个布局放在第一个布局的下面,但是除非第一个布局始终保持恒定的高度,否则尺寸会有所不同。

If you know you always want a 70:30 ratio regardless of the first layout height, you can use layout_weights instead. 如果您知道始终要使用70:30的比例,而不考虑第一个布局高度,则可以改用layout_weights I can add that in if that's what you're looking for instead. 如果您要查找的是我可以添加的内容。

To have a constant ratio, change your root layout to be LinearLayout instead of RelativeLayout then set the orientation to be vertical and a weightSum to 1. 要使比例恒定,请将根布局更改为LinearLayout而不是RelativeLayout然后将方向设置为垂直,并将weightSum为1。

Then set your first LinearLayout to be a layout_weight of 0.7 and your second LinearLayout to be a layout_weight of 0.3. 然后将您的第一个LinearLayout设置为0.7的layout_weight ,将第二个LinearLayout设置为0.3的layout_weight Like this: 像这样:

<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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM