在Android布局中使视图可调整大小

时间:2016-02-02 14:45:19

标签: android

我已经为我的应用主要活动设计了一个布局,其中包括精选图片和一些可滚动按钮。他们目前在dp中有固定的大小值。但我希望以一种方式对它们进行编码,如果应用程序在较小的屏幕上呈现,则所有其他视图保持相同的大小,但按钮和特色图像会将自身调整为可用的剩余空间。这是代码和图片:

<ScrollView 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: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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="ir.dadhesab.dadhesab.MainActivity"
tools:showIn="@layout/activity_main"
android:background="@color/background"
xmlns:ads="http://schemas.android.com/apk/res-auto"

>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:src="@drawable/slider1"
        android:adjustViewBounds="true"
        android:layout_weight="1"
        android:layout_gravity="center_horizontal"
        tools:ignore="ContentDescription"
        />


    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/horizontalScrollView"
        android:layout_weight="1"
        >
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="1">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <Button
                    android:id="@+id/tile1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:background="@drawable/tile1"
                    android:onClick="OpenCostofHearing"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_costof_hearing"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"

                >
                <ImageView
                    android:id="@+id/tile2"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile2"
                    android:onClick="OpenDowry"
                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_dowry"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile3"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile3"
                    android:onClick="OpenLatePaymentDamage"
                    tools:ignore="ContentDescription"

                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_late_payment_damage"
                    android:gravity="center"
                    android:textColor="@color/text"


                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile3_1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile3_1"
                    android:onClick="OpenHonorarium"
                    tools:ignore="ContentDescription"

                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_honorarium"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile4"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile4"
                    android:onClick="OpenAttorneyCharges"
                    tools:ignore="ContentDescription"

                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_attorney_charges"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile4_1"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile4_1"
                    android:onClick="OpenExpertWage"
                    tools:ignore="ContentDescription"

                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_expert_wage"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile5"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile5"
                    android:onClick="OpenBloodMoney"
                    tools:ignore="ContentDescription"

                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_blood_money"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="10dp"
                >
                <ImageView
                    android:id="@+id/tile6"
                    android:layout_width="120dp"
                    android:layout_height="120dp"
                    android:src="@drawable/tile6"
                    android:onClick="OpenInheritance"
                    tools:ignore="ContentDescription"


                    />
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/title_activity_inheritance"
                    android:gravity="center"
                    android:textColor="@color/text"

                    />

            </LinearLayout>


        </LinearLayout>
    </HorizontalScrollView>

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1">
    </com.google.android.gms.ads.AdView>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/copyrightnotice"
        android:id="@+id/copyrightNotice"
        android:gravity="center"
        android:padding="15dp"
        android:layout_weight="1"
        android:textColor="@color/text"

        android:textSize="12dp"
        android:textIsSelectable="true" />
</LinearLayout>

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以使用weightSum和layout_weight来实现您想要的效果。它保持所有屏幕尺寸的比例。

查看以下链接

stackoverflow

developer.android