【问题标题】:Multiple layouts for tablet and phone平板电脑和手机的多种布局
【发布时间】:2017-12-13 04:33:21
【问题描述】:

我正在为平板电脑和手机开发一个安卓应用。因此,为了实现这一点,我为手机(layout)创建了两个文件夹,为平板电脑布局创建了一个文件夹(layout-sw600dp)。

根据我之前的问题comment,我假设只有平板电脑会从layout-sw600dp 获取布局。但这似乎不起作用。

对于nexus 4, nexus 5, nexus 6, nexus 7 设备,它采用layout-sw600dp 布局。

我在这里缺少什么?如何计算手机/平板电脑大小以适合 DP 布局?

平板电脑和手机所需的设计:

另一个退休使用两个独立的布局:

手机设计:

标签设计:

针对下面answer 的回复帖子,我有一个这样的线性布局:

  <LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/linearLayouttest">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/black"
        android:gravity="left"
        android:text="LabelFirstName"
        android:layout_marginTop="@dimen/_8sdp"
        android:textSize="@dimen/_13sdp"
        android:id="@+id/firstNameLabel"
        android:layout_marginLeft="@dimen/_15sdp"
        android:layout_marginRight="@dimen/_15sdp" />
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/firstName"
        android:layout_marginTop="@dimen/_3sdp"
        android:background="@drawable/edt_bg"
        android:textColor="@color/black"
        android:layout_marginRight="@dimen/_15sdp"
        android:layout_marginLeft="@dimen/_15sdp"
        android:padding="@dimen/_5sdp"
        android:textSize="@dimen/_14sdp"
        android:textColorHint="@color/textColor" />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="center_horizontal"
        android:textColor="@color/black"
        android:gravity="left"
        android:text="LabelLastName"
        android:layout_marginTop="@dimen/_3sdp"
        android:textSize="@dimen/_13sdp"
        android:id="@+id/lastNameLabel"
        android:layout_marginLeft="@dimen/_15sdp"
        android:layout_marginRight="@dimen/_15sdp" />
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/lastName"
        android:layout_marginTop="@dimen/_3sdp"
        android:background="@drawable/edt_bg"
        android:textColor="@color/black"
        android:layout_marginRight="@dimen/_15sdp"
        android:layout_marginLeft="@dimen/_15sdp"
        android:padding="@dimen/_5sdp"
        android:textSize="@dimen/_14sdp"
        android:textColorHint="@color/textColor" />
</LinearLayout>

当我改变方向时,文本视图不可见。有什么解决办法吗?

【问题讨论】:

  • 您可以通过使用不同的尺寸文件来实现该目的,而不是创建多个布局。尝试this。
  • 我试过了,但它不起作用,因为我有不同的电话和桌子设计。请检查我编辑的问题。

标签: android android-layout layout


【解决方案1】:

您可以使用https://github.com/intuit/sdp 库根据所有设备(手机和平板电脑)设置视图尺寸。它将根据设备屏幕大小自动调整大小。设置如下:

<TextView
    android:id="@+id/tv_username"
    android:layout_width="@dimen/_45sdp"
    android:layout_height="@dimen/_45sdp"
    android:text="@string/chat"
    android:textColor="@color/colorPrimary"/>

更新

首先检查设备是手机还是平板使用Determine if the device is a smartphone or tablet?

或者您可以使用下面的方法来确定

public boolean isTablet() {
try {
    // Compute screen size
    DisplayMetrics dm = context.getResources().getDisplayMetrics();
    float screenWidth  = dm.widthPixels / dm.xdpi;
    float screenHeight = dm.heightPixels / dm.ydpi;
    double size = Math.sqrt(Math.pow(screenWidth, 2) + Math.pow(screenHeight, 2));
    // Tablet devices should have a screen size greater than 6 inches
    return size >= 6;

} catch(Exception e) {
    e.printStackTrace();
    return false;
}

}

然后以编程方式设置父 LinearLayout 方向,如下所示:

boolean tabletSize = getResources().getBoolean(R.bool.isTablet);
if (tabletSize) {
    LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
    ll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
    ll.setOrientation(LinearLayout.HORIZONTAL);
} else {
    LinearLayout ll = (LinearLayout) findViewById(R.id.linearLayout);
    ll.setLayoutParams(LinearLayout.WRAP_CONTENT, LinearLayout.WRAP_CONTENT);
    ll.setOrientation(LinearLayout.VERTICAL);
}

XML 更正

您的布局必须如下所示:

<LinearLayout
    android:id="@+id/linearLayouttest"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/firstNameLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginTop="@dimen/_8sdp"
            android:gravity="left"
            android:text="LabelFirstName"
            android:textColor="@color/black"
            android:textSize="@dimen/_13sdp" />

        <EditText
            android:id="@+id/firstName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginTop="@dimen/_3sdp"
            android:background="@drawable/edt_bg"
            android:padding="@dimen/_5sdp"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="@dimen/_14sdp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/lastNameLabel"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginTop="@dimen/_3sdp"
            android:gravity="left"
            android:text="LabelLastName"
            android:textColor="@color/black"
            android:textSize="@dimen/_13sdp" />

        <EditText
            android:id="@+id/lastName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/_15sdp"
            android:layout_marginRight="@dimen/_15sdp"
            android:layout_marginTop="@dimen/_3sdp"
            android:background="@drawable/edt_bg"
            android:padding="@dimen/_5sdp"
            android:textColor="@color/black"
            android:textColorHint="@color/black"
            android:textSize="@dimen/_14sdp" />

    </LinearLayout>
</LinearLayout>

【讨论】:

  • 请检查我编辑的问题。我需要不同的手机和平板电脑设计
  • 让我们猜测您为 HDPI 设备设置了 25sdp 的 editText textSize 然后它将根据其他设备大小自动调整您的布局大小
  • 两个 texViews 是否适合使用这个的平板电脑排成一行?
  • 感谢您的回答,但如果我有两个文本视图怎么办?如果在手机布局中,textvies 应该在另一个居中对齐的下方排成一行,而在选项卡布局中,一个必须左对齐和右对齐?
  • 请检查新更新“XML CORRECTION”以更正您的 XML 文件
【解决方案2】:

基本上 xxhdpi 和 xhdpi 适用于 5" 到 5"5' 设备。对于平板电脑,您可以使用

sw600dp

sw600dp-tvdpi

sw720dp

sw720dp-mdpi

sw720dp-xhdpi

。我一直在研究这个,这些是标签所采用的值。试一次。希望它会起作用。

【讨论】:

  • 这是否意味着我必须创建 sw600dp,sw600dp-tvdpi,sw720dp,sw720dp-mdpi,sw720dp-xhdpi 文件夹?
  • 是的,您必须创建目录,如 values-sw600dp-tvdpi、values-sw600dp 等,然后您必须放置尺寸...
  • 还有一件事,这些 values 目录必须在 res 目录下创建,而不是在 values 或 layout 下。
  • 我很困惑。我需要在所有这些文件夹中创建dimens xml 吗?我已经创建了。我的问题是平板电脑布局我需要放置哪个文件夹?
  • 听你需要在所有这个文件夹下创建dimen.xml。假设在您的值的文件夹中 dimen 您有一个类似 150dp 的键,然后将相同的键放在您在 dimen.xml 的其余部分中使用的每个 dimen.xml 上并设置值accordig根据您的需要
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
  • 2011-10-31
  • 2014-07-27
  • 2019-03-20
相关资源
最近更新 更多