【问题标题】:Android layout customizeAndroid布局自定义
【发布时间】:2014-09-20 00:02:23
【问题描述】:

我刚开始进行 Android 开发,但布局仍然存在一些问题。

这是我想要的渲染:
(来源:hostingpics.net

我做了这个,但我真的不明白我怎么能做这种布局。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp"
    android:minWidth="25px"
    android:minHeight="25px">
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="90px"
        android:id="@+id/linearLayout1">
        <ImageView
            android:src="@android:drawable/ic_menu_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/imageView1" />
    </LinearLayout>
    <LinearLayout
        android:orientation="horizontal"
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="fill_parent"
        android:layout_height="90px"
        android:id="@+id/linearLayout2">
        <TextView
            android:text="Bonus1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView1"
            android:layout_gravity="center" />
        <TextView
            android:text="Bonus2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/textView2"
            android:layout_gravity="center" />
    </LinearLayout>
</LinearLayout>

【问题讨论】:

  • 你的问题是什么?
  • 使用RelativeLayout而不是LinearLayout来开发附加屏幕
  • RelativeLayout 是您最好的选择。请注意,像所有项目都是方形的任何约束都不能在静态布局中完成,并且需要一些代码。自定义ViewGroup 也不会太难开发,但如果您刚刚接触 Android 开发,可能会有点难。

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

使用线性布局


XML-CODE::

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:orientation="horizontal"
        android:padding="5dp"
        android:weightSum="2" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FFFFFF"
            android:padding="10dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="#000000"
                android:padding="5dp" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"
                    android:gravity="bottom|center" >

                    <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="2dp"
                        android:layout_marginLeft="5dp"
                        android:layout_marginRight="5dp"
                        android:text="TextView"
                        android:textStyle="bold" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="5dp"
                android:layout_weight="1" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="#FFFFFF"
                    android:orientation="vertical"
                    android:weightSum="2" >

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:background="#FFFFFF"
                        android:orientation="vertical" >

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="5dp"
                            android:layout_weight="1"
                            android:background="#000000"
                            android:padding="5dp" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:gravity="bottom|center" >

                                <TextView
                                    android:id="@+id/textView2"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="2dp"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginRight="5dp"
                                    android:text="TextView"
                                    android:textStyle="bold" />
                            </LinearLayout>
                        </LinearLayout>

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_margin="5dp"
                            android:layout_weight="1"
                            android:background="#000000"
                            android:padding="5dp" >

                            <LinearLayout
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="#FFFFFF"
                                android:gravity="bottom|center" >

                                <TextView
                                    android:id="@+id/textView1"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginBottom="2dp"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginRight="5dp"
                                    android:text="TextView"
                                    android:textStyle="bold" />
                            </LinearLayout>
                        </LinearLayout>
                    </LinearLayout>
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#FFFFFF"
        android:gravity="bottom|center" >
    </LinearLayout>

</LinearLayout>

Snapshot


注意:: 使用此代码并自定义您希望的方式

希望这有帮助!

【讨论】:

  • +1 因为它是通用 xml。它可以用于任何尺寸的设备。
【解决方案2】:

对这种方法使用 Gridlayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".TestActivity" >

    <com.example.testview.SquareView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"
        android:background="@android:color/white"
        android:text="Largetext" />

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <com.example.testview.SquareView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginBottom="3dp"
            android:layout_marginLeft="3dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:text="smallText" />

        <com.example.testview.SquareView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_marginLeft="3dp"
            android:layout_marginTop="3dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:padding="5dp"
            android:text="smallText" />
    </LinearLayout>

</LinearLayout>

自定义方形文本视图

package com.example.testview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.TextView;

public class SquareView extends TextView {

    public SquareView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SquareView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareView(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
    }

}

【讨论】:

    【解决方案3】:

    推荐使用 ConstraintLayout,因为 ConstraintLayout 可以让您创建具有平面视图层次结构的大型复杂布局。阅读有关 ConstraintLayout 的更多信息here

    使用ConstraintLayout的解决方案

    <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
    
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:padding="16dp">
    
            <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guide_line_1"
                    android:layout_width="1dp"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintGuide_percent="0.5" />
    
            <androidx.constraintlayout.widget.Guideline
                    android:id="@+id/guide_line_2"
                    android:layout_width="1dp"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal"
                    app:layout_constraintGuide_percent="0.5" />
    
            <TextView
                    android:id="@+id/text_view_1"
                    android:layout_width="0dp"
                    android:layout_height="0dp"
                    android:layout_marginEnd="8dp"
                    android:ellipsize="end"
                    android:text="@string/big_lorem"
                    app:layout_constraintBottom_toBottomOf="@id/text_view_3"
                    app:layout_constraintEnd_toEndOf="@id/guide_line_1"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="@id/text_view_2" />
    
            <TextView
                    android:id="@+id/text_view_2"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginBottom="8dp"
                    android:text="@string/small_lorem"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="@id/guide_line_1"
                    app:layout_constraintTop_toTopOf="parent" />
    
            <TextView
                    android:id="@+id/text_view_3"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:layout_marginBottom="16dp"
                    android:text="@string/small_lorem"
                    app:layout_constraintBottom_toBottomOf="@id/guide_line_2"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="@id/guide_line_1"
                    app:layout_constraintTop_toBottomOf="@id/text_view_2" />
    
        </androidx.constraintlayout.widget.ConstraintLayout>
    
    </layout>
    

    设计视图中的最终布局如下所示。

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 2016-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多