Android布局自定义

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

我刚开始从事 Android 开发,但在布局方面仍然存在一些问题。

这是我想要的渲染: render
(来源: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>

最佳答案

使用线性布局


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>

快照

enter image description here


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

希望这对您有所帮助!!

关于Android布局自定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25943934/

相关文章:

android - Moshi LinkedHashTreeMap 类使用 Proguard 抛出异常

android - 了解 Android 多设备布局

java - CardView-TextView marginRight

Android LinearLayout 在实际设备上看起来不一样

java - 从 Firebase 实时数据库中删除用户数据

android - 无法使用 RxJava 和 Reterofit2 创建端点

android - ActionBar.Tab 的自定义 View 显示不正确

android - textview 的重力属性未使文本居中

Android:为什么屏幕上没有任何显示?

android - 将多个 View 添加到 LinearLayout 的最快方法是什么?