【问题标题】:android supporting different resolution with layout folderandroid支持不同分辨率的布局文件夹
【发布时间】:2018-11-14 10:53:44
【问题描述】:

我正在为应用程序创建布局,但问题是布局在所有大型设备上都很完美,但不适用于 hdpi。所以我想为布局创建单独的 hdpi 文件夹。我选择 android 资源目录并选择密度作为 hdpi 但不适用于仅 hdpi 设备。我也尝试了尺寸和屏幕宽度,但没有工作。你能帮我改进一下吗?

布局代码

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/combine_bg"
        android:layout_marginTop="@dimen/dp20"
        android:layout_marginBottom="@dimen/dp20"
        android:layout_marginRight="@dimen/dp20">


        <TextView
            android:id="@+id/txtWelcomeTo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/welcome_to"
            android:layout_marginTop="@dimen/dp170"
            android:layout_marginLeft="@dimen/dp10"
            android:textSize="@dimen/dp15"
            android:textColor="@android:color/black" />


        <TextView
            android:id="@+id/txtPrivateCare"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/the_private_care_masters"
            android:layout_marginLeft="@dimen/dp10"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:layout_below="@id/txtWelcomeTo" />


        <EditText
            android:id="@+id/etemail"
            android:layout_width="@dimen/dp190"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtPrivateCare"
            android:layout_marginLeft="@dimen/dp10"
            android:hint="@string/email_phone"
            android:layout_marginTop="@dimen/dp20" />


        <EditText
            android:id="@+id/etPassword"
            android:layout_width="@dimen/dp190"
            android:layout_height="wrap_content"
            android:layout_below="@id/etemail"
            android:layout_marginLeft="@dimen/dp10"
            android:layout_marginTop="@dimen/dp10"
            android:hint="@string/password"

            />


        <ImageButton
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/login_btn"
            android:background="@android:color/transparent"
            android:layout_below="@id/etemail"
            android:layout_marginLeft="@dimen/dp25"
            android:layout_toRightOf="@id/etemail" />


        <CheckBox
            android:id="@+id/chkRememberMe"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/etPassword"
            android:layout_marginLeft="@dimen/dp10"
            android:layout_marginTop="@dimen/dp10"
            android:text="@string/remember_me"
            android:textSize="@dimen/dp12"

            />


        <TextView
            android:id="@+id/txtForgotPwd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/chkRememberMe"
            android:text="@string/forgot_pwd"
            android:layout_below="@id/etPassword"
            android:layout_marginTop="@dimen/dp17"
            android:textSize="@dimen/dp12"
            android:layout_marginLeft="@dimen/dp15"

            />


        <TextView
            android:id="@+id/txtSignUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/not_have_account"
            android:layout_below="@id/txtForgotPwd"
            android:layout_marginTop="@dimen/dp30"
            android:textSize="@dimen/dp12"
            android:layout_marginLeft="@dimen/dp15"

            />


    </RelativeLayout>
</RelativeLayout>

以及我要创建的 .psd 的屏幕截图

还有我在 hdpi 设备上的布局:

我该如何解决这个问题,请帮忙

dimens.xml

<dimen name="dp1">1dp</dimen>
    <dimen name="dp2">2dp</dimen>
    <dimen name="dp3">3dp</dimen>
    <dimen name="dp4">4dp</dimen>

dimens.xml-大

<dimen name="dp1">1.5dp</dimen>
<dimen name="dp2">3.0dp</dimen>
<dimen name="dp3">4.5dp</dimen>
<dimen name="dp4">6.0dp</dimen>

dimes.xml - xlarge

 <dimen name="dp1">2.0dp</dimen>
    <dimen name="dp2">4.0dp</dimen>
    <dimen name="dp3">6.0dp</dimen>

【问题讨论】:

  • 您使用spssp 设置文本大小吗??
  • 请发布您的dimens.xml 文件。
  • @AndréSousa 检查我编辑的代码
  • @MohammadAli 我使用 dp 作为文本大小
  • 请使用 ssp 的文本大小和任何其他东西,如边距、填充,使用 sdp 这两件事你可以找到图书馆 @mishti

标签: android android-studio android-layout android-fragments android-intent


【解决方案1】:

您应该使用sp 而不是dp 作为您的文本大小。

来自文档:

sp

与比例无关的像素 - 这类似于 dp 单位,但它也根据用户的字体大小偏好进行缩放。建议您在指定字体大小时使用此单位,以便根据屏幕密度和用户偏好进行调整。

【讨论】:

  • 对@Andre 如果不知道sspsdp 请参考:LINK。只是为了您的信息。
  • @MohammadAli 谢谢!我会检查一下;)
【解决方案2】:

请使用 GitHub 的 SDP 库。通过使用这个库,您无需自动处理所有 android 设备分辨率。您只需创建一个唯一的 UI 文件。 这个库非常简单,可以在项目资源文件夹中的所有维度值文件夹中使用,或者您可以添加它的依赖项,如下所示。

dependencies {
  implementation 'com.intuit.sdp:sdp-android:1.0.6'
}

您也可以将 SSP GitHub lib 用于您的 TextSize。与 SDP 库相同

dependencies {
  implementation 'com.intuit.ssp:ssp-android:1.0.6'
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 2015-09-23
    • 1970-01-01
    • 2013-05-18
    • 1970-01-01
    相关资源
    最近更新 更多