Android 平板电脑和手机的多种布局
我正在为平板电脑和手机开发一款android应用程序。为此,我在上为手机创建了两个文件夹(Android 平板电脑和手机的多种布局,android,android-layout,layout,Android,Android Layout,Layout,我正在为平板电脑和手机开发一款android应用程序。为此,我在上为手机创建了两个文件夹(layout),为平板电脑布局创建了一个文件夹(layout-sw600dp) 根据我之前的问题,我假设只有平板电脑才能从layout-sw600dp中获取布局。但它似乎不起作用 对于nexus 4、nexus 5、nexus 6、nexus 7设备,它采用的是layout-sw600dplayout 我错过了什么?如何计算手机/平板电脑的大小,使其符合适当的DP布局 平板电脑和手机所需的设计: 另一种方
layout
),为平板电脑布局创建了一个文件夹(layout-sw600dp
)
根据我之前的问题,我假设只有平板电脑才能从layout-sw600dp
中获取布局。但它似乎不起作用
对于nexus 4、nexus 5、nexus 6、nexus 7设备,它采用的是layout-sw600dp
layout
我错过了什么?如何计算手机/平板电脑的大小,使其符合适当的DP
布局
平板电脑和手机所需的设计:
另一种方法是使用两种不同的布局:
手机设计:
选项卡设计:
在回答下面的帖子时,我采用了如下线性布局:
<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
android:id="@+id/tv_username"
android:layout_width="@dimen/_45sdp"
android:layout_height="@dimen/_45sdp"
android:text="@string/chat"
android:textColor="@color/colorPrimary"/>
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);
}
}
然后按如下程序设置父线性布局方向:
<TextView
android:id="@+id/tv_username"
android:layout_width="@dimen/_45sdp"
android:layout_height="@dimen/_45sdp"
android:text="@string/chat"
android:textColor="@color/colorPrimary"/>
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>
基本上xxhdpi和xhdpi适用于5英寸到5英寸的设备。对于您可以使用的平板电脑 sw600dp sw600dp tvdpi sw720dp sw720dp mdpi sw720dp xhdpi
。我一直在研究这个问题,这些是tabs采用的值。试试看。希望它能起作用。不用创建多个布局,你可以通过使用不同的dimens文件来实现。试试。我试过了,但不起作用,因为我对手机和桌子有不同的设计。请检查我编辑的问题。请检查我编辑的问题。我需要不同的手机和桌面设计猜测您为HDPI设备设置了25sdp的editText textSize,然后它会根据其他设备大小自动调整布局使用此功能时,平板电脑的两个TexView可以放在一行中吗?谢谢您的回答,但如果我有两个TextView呢?如果在手机布局中,文本应该在另一个中心对齐的下方一行,而在选项卡布局中,文本必须左对齐,右对齐?请检查新的更新“XML更正”以获得XML文件更正这是否意味着我必须创建sw600dp、sw600dp tvdpi、sw720dp、sw720dp mdpi,sw720dp xhdpi文件夹?是的,您必须创建目录,如values-sw600dp-tvdpi、values-sw600dp等,然后您必须放置dimens…还有一件事,这些values目录必须在res目录下创建,而不是在values或layout下。我感到困惑。我需要在所有这些文件夹中创建dimens xml?我已经创造了。我的问题是关于tablet layout,我需要将layout放在哪个文件夹中?听着,你需要在这个文件夹下创建dimen.xml。假设在值的文件夹dimen中有一个类似150dp的键,然后在dimen.xml的其余部分中使用的每个dimen.xml上放置相同的键,并根据需要设置值