简体   繁体   English

需要有关Android布局的说明

[英]Need explanation about Android layouts

I want to create a view like this : 我想创建一个这样的视图:

在此输入图像描述

I tried this layout architecture : 我尝试了这种布局架构:

<RelativeLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView> // The blue top bar

    <ScrollView android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" ></ScrollView>

    <LinearLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:orientation="vertical">

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Description"
            android:gravity="center_horizontal">
        </TextView>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Place"
            android:gravity="center_horizontal">
        </TextView>

        <TextView android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal">
        </TextView>

    </LinearLayout>

    <RelativeLayout android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:gravity="bottom">

        <ImageView android:layout_width="wrap_content"
           android:layout_height="40dp"
           android:clickable="false"
           android:scaleType="center" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:gravity="left"
            android:orientation="horizontal" >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:text="Button 1" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:text="Button 2" />

            <Button
                android:layout_width="wrap_content"
                android:layout_height="30dp"
                android:layout_gravity="center_vertical"
                android:text="Button 3" />

        </LinearLayout>

    </RelativeLayout>

</RelativeLayout>

But this won't work for some reasons, first it doesn't display the buttons as I want in the bottom bar. 但由于某些原因,这不起作用,首先它不会在底栏中显示我想要的按钮。

I'm a beginner at Android dev so please help me. 我是Android开发人员的初学者,所以请帮助我。

  1. Assign id to "Place" component, so You could refer to this id later, when placing buttons: id分配给“Place”组件,以便稍后在放置按钮时可以引用此id:

      <LinearLayout android:id="@+id/placeLayout" ... /> 
  2. In relative layout, You could tell your buttons to be under component with id=placeLayout in this way: 在相对布局中,您可以通过以下方式告诉您的按钮位于id = placeLayout的组件下:

      <Button android:id="@+id/button1" android:layout_below="@+id/placeLayout" ... /> 
  3. If you want one button to be right of another and under placeLayout component, write it: 如果您希望一个按钮与另一个按钮正确并位于placeLayout组件下,请将其写入:

      <Button android:id="@+id/button2" android:layout_below="@+id/placeLayout" android:layout_toRightOf="@+id/button1" ... /> <Button android:id="@+id/button3" android:layout_below="@+id/placeLayout" android:layout_toRightOf="@+id/button2" ... /> 

Here is complete list of parameters You could use to "tell" each element where it has to be placed: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 以下是完整的参数列表您可以用来“告诉”每个元素必须放在哪里: http//developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

My suggestion, use a linear layout and play with "android:weightSum" (main layout) and "android:layout_weight"(inner views) attributes, it might be complicated at the very first moment, but later it will be the best approach to fit any screen. 我的建议,使用线性布局并使用“android:weightSum”(主要布局)和“android:layout_weight”(内部视图)属性,它可能在第一时间很复杂,但后来它将是最好的方法适合任何屏幕。 Check this thread: Linear Layout and weight in Android 检查此线程: Android中的线性布局和权重

Using that technique you are telling to each inner view (or layout) inside the main layout how many space it should fill. 使用该技术,您将告诉主布局内的每个内部视图(或布局)应填充多少空间。

ie: if the main layout has android:weightSum=1 and every inner view has android:layout_weight=0.33 it menss that every inner view fill 1/3 of the total space available. 即:如果主要布局有android:weightSum = 1并且每个内部视图都有android:layout_weight = 0.33,那么它会强调每个内部视图填充可用总空间的1/3。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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