我有布局问题,不知道如何解决。
我喜欢在屏幕上显示webview和2个按钮。 我使用包含webview的LinearLayout和另一个包含2个按钮的linearlayout来定义它。
到目前为止看起来非常好。由于较新的设备有更大的屏幕尺寸,我的屏幕看起来非常难看 因为我的按钮太小了 保持2个按钮的内部线性布局用layout_height =“60px”定义,我猜这是我的问题。
您如何在任何设备上将其设置为良好?是否有可能根据屏幕尺寸定义尺寸? 或者我应该在运行时将内部线性布局的高度设置为screenheigh的因子吗?
这样做的最佳方式是什么?
问候
屏幕截图显示我的按钮在高清屏幕上太小。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:id="@+id/textviewerlayout"
android:orientation="vertical">
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="60px"
android:layout_width="fill_parent"
android:background="#FFFFFF"
android:id="@+id/textviewerbuttonlayout"
android:orientation="horizontal">
<Button
android:id="@+id/vorher"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:text="" />
<Button
android:id="@+id/nachher"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="0"
android:text="" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:2)
而不是使用px,使用dp。这将使用与密度无关的像素,这些像素是根据屏幕尺寸和分辨率按设备计算的。查看here了解更多信息。由于高清屏幕使用更高密度的像素,因此您的按钮由于其硬编码像素大小而显得更小。使用dp(或dip)将使它们在多个设备上的大小大致相同。
答案 1 :(得分:1)
像Jorgan所说使用dp而不是px。
看看它是否正在寻找:
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textviewerbuttonlayout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFFFFF"
android:orientation="horizontal" >
<Button
android:id="@+id/vorher"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="" />
<Button
android:id="@+id/nachher"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="" />
</LinearLayout>
使用重量来确定高度会使它在任何尺寸下看起来都很好,但是如果你想为平板电脑或景观做出特定的布局,你应该把它放在他们的文件夹中。
答案 2 :(得分:0)
http://developer.android.com/guide/practices/screens_support.html
检查主题最佳实践
下的上述链接以下是有关如何确保应用程序在不同屏幕上正常显示的快速清单:
在XML布局文件中指定尺寸时,请使用wrap_content,fill_parent或dp单位。
请勿在应用程序代码中使用硬编码像素值
您可以使用单一相对布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<WebView
android:id="@+id/mywebview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/button1"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="67dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="57dp"
android:text="Button" />
</RelativeLayout>