android - 平板电脑和手机的多种布局

标签 android android-layout layout

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

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

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

我在这里错过了什么?如何根据正确的 DP 布局计算手机/平板电脑的尺寸?

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

enter image description here

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

手机设计:

enter image description here

标签设计:

enter image description here

回应下面的回复帖子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>

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

最佳答案

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

<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>

关于android - 平板电脑和手机的多种布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47785527/

相关文章:

android - 需要将数据库回滚到以前的状态

javascript - 使用 javascript 的 Android 导航包装

android - 您如何为多种 Android 屏幕尺寸制作布局?

layout - 基于同级最大宽度显示小部件

java - Android/Eclipse 错误 - "Could not find class ' org.jivesoftware.smack.ConnectionConfiguration',从方法引用”

java - Android幻灯片放映导致内存泄漏

android - 透明 RelativeLayout 容器内的不透明 TextView

java - Activity 不会实例化

android:bottomsheet 不会正常消失

CSS 内容区域 100% 高度