我正在设计一个活动,要在其中放置5个buttons,为此,我想设计一个系统的布局以包含所有这些按钮,如下图所示。android - 如何在Android中使布局组件具有响应性?-LMLPHP

我已经成功地通过硬编码值设计了这种布局。但是,在不同尺寸的屏幕上进行测试时,会导致UI损毁。我的问题是,如果我想为多种屏幕分辨率设计应用程序UI,该怎么办?有什么方法可以使用wrap_contentmatch_parent进行设计?通过搜索,我得到了一个解决方案,我们可以为单个活动设计适合屏幕尺寸的不同布局。但是可靠吗?
我的xml代码如下。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/gradient"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="30dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/upbtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="90dp" />

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


        <Button
            android:id="@+id/leftbtn"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="100dp"
            android:layout_height="150dp" />


        <Button
            android:id="@+id/centerbtn"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="100dp"
            android:layout_height="150dp"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"/>


        <Button
            android:id="@+id/rightbtn"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"/>

    </LinearLayout>

    <Button
        android:id="@+id/downbtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="90dp" />
    </LinearLayout>
</LinearLayout>

最佳答案

使用此布局,我根据您的需要添加了权重,并为所有符合您要求的视图添加了相关的高度和宽度。经过所有设备的测试,看起来不错。删除了第一个布局的边距顶部,但是您可以根据需要保留它。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

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

    <Button
        android:id="@+id/upbtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="0dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:weightSum="3"
        android:orientation="horizontal">

        <Button
            android:id="@+id/leftbtn"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent" />


        <Button
            android:id="@+id/centerbtn"
            android:layout_weight="1"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />


        <Button
            android:id="@+id/rightbtn"
            android:layout_weight="1"
            style="@style/Widget.AppCompat.Button.Colored"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            />

    </LinearLayout>

    <Button
        android:layout_weight="1"
        android:id="@+id/downbtn"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_width="match_parent"
        android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>

关于android - 如何在Android中使布局组件具有响应性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45869714/

10-08 23:54
查看更多