【问题标题】:Supporting multiple screens in android in single layout在单一布局中支持 android 中的多个屏幕
【发布时间】:2023-03-19 01:50:01
【问题描述】:

我已点击以下链接,以便我的应用程序可以支持不同的屏幕尺寸:

Supporting multiple screens in android

该解决方案完美运行。但我在这里担心的是,当我有一个有 8-9 个屏幕的 android 应用程序时,这意味着我将有 8-9 个不同的 .xml 布局文件。现在要通过文件夹分叉支持所有屏幕,这意味着我已经管理了近 50 个用于布局的 xml 文件,并且对于 UI 的简单更改,我必须转到所有文件夹并在 xml 文件中实现该更改。那么有没有更好的方法,我的意思是这样的布局可以自己调整控件的大小或类似的东西?

【问题讨论】:

    标签: java android android-layout android-ui


    【解决方案1】:

    我认为这并不太复杂。在布局文件夹中创建所有布局。使用styles.xml, dimens.xml and strings.xml 保存字体大小和字符串。当您的布局完成后,即不需要更改,然后从布局文件夹中复制所有这些布局并粘贴到layout-small, layout-large, layout-xlarge。因此,当您需要更改字符串、样式和字体大小时,您必须仅在值文件夹中进行更改。

    例如-

    使用android:text="string/hello" 代替android:text="Hello" 并将hello 的值保存在strings.xml 中。同样的文字大小android:textSize="@dimen/btxt"

    这是最好的选择之一。

    【讨论】:

    • 基本上我想要的是,我不想拥有单个屏幕的多个布局文件。我想要一些东西,比如我编写 layout.xml 文件,在该文件中,我使用了这样的布局,它将根据屏幕大小调整我的 UI 组件。就像我们在 java swing 应用程序中有 Bag 布局一样。
    • 我知道您需要根据屏幕尺寸进行计算。然后据此更改您的布局组件。但我不知道通过使用这种方式,你在每个屏幕上的布局都很好看。
    • 在此链接中检查一些基于计算的答案。希望它可以帮助你。 stackoverflow.com/questions/21274692/font-size-of-text
    【解决方案2】:

    我创建了一个相对大小单位。 此尺寸单位可用于为所有屏幕构建一个布局 xml 文件。 此尺寸单位可通过链接sdp sdk 获得。 以下是使用此 sdk 构建的布局 XML 示例:

    <RelativeLayout 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:background="@android:color/white"
    android:gravity="center">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    
        <LinearLayout
            android:id="@+id/give_us_a_review_landmine_main_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical"
            android:paddingBottom="@dimen/_27sdp"
            android:paddingLeft="@dimen/_43sdp"
            android:paddingRight="@dimen/_43sdp"
            android:paddingTop="@dimen/_50sdp" >
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Intuit"
                android:textColor="@android:color/black"
                android:textSize="@dimen/_40sdp"/>
    
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/_minus10sdp"
                android:paddingBottom="@dimen/_15sdp"
                android:orientation="horizontal" >
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:includeFontPadding="false"
                    android:text="♡"
                    android:textColor="#ED6C27"
                    android:textSize="@dimen/_70sdp"
                    android:textStyle="bold" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:includeFontPadding="false"
                    android:text="U"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/_70sdp" />
            </LinearLayout>
    
            <TextView
                android:id="@+id/give_us_a_review_landmine_text_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:paddingBottom="@dimen/_12sdp"
                android:text="Rate us so we can grow and help more people get their finances in check"
                android:textColor="@android:color/black"
                android:textSize="@dimen/_16sdp" />
    
            <TextView
                android:id="@+id/give_us_a_review_landmine_text_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="★★★★★"
                android:textColor="#747474"
                android:textSize="@dimen/_22sdp"
                android:textStyle="bold" />
    
            <Button
                android:id="@+id/give_us_a_review_landmine_button"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="@dimen/_25sdp"
                android:padding="@dimen/_8sdp"
                android:text="Rate"
                android:textSize="@dimen/_15sdp"
                android:visibility="visible"
                android:textColor="@android:color/white"
                android:gravity="center"
                android:minWidth="120dp"
                android:includeFontPadding="false"
                android:background="#0ac775"
                android:singleLine="true" />
    
        </LinearLayout>
    </LinearLayout>
    

    结果如下:

    请注意,UI 元素会随屏幕大小缩放。

    【讨论】:

      【解决方案3】:

      看看这个问题:LINK

      然后您可以创建一个包含所有 XML 布局中的通用内容的单个 XML 文件,然后对于每个布局只包含或合并所需的通用 XML 部分,这样您只需编辑通用 XML 文件一次即可然后布局将包含新的更改。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 2011-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多