【发布时间】:2015-02-25 11:38:50
【问题描述】:
我正在构建一个 android 应用程序,并且我有一个对话框片段。对话框片段具有设定的宽度。但是,问题是当应用在具有不同屏幕尺寸的设备上运行时,对话框片段没有正确居中。
最初,我拥有的是:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="500dp"
android:layout_height="wrap_content">
<-- code goes here -->
</RelativeLayout>
如您所见,我有一个定义宽度的 relativeLayout。因为我知道你不能在相对布局中使用layout_weight,所以我所做的就是将父相对布局包装在线性布局中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.8"
android:weightSum="1">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
但是,这不起作用,因为当我在屏幕尺寸较小的设备上运行应用程序时,对话框片段会被剪切。
如何将对话框片段的宽度设置为屏幕大小的百分比?这有可能吗,还是我必须求助于以编程方式设置它?
【问题讨论】:
-
如果您将
RelativeLayoutandroid:layout_width设置为match_parent它应该很好地集中在所有设备上 -
设置重心
-
@Marcus,即使relativeLayout的宽度大于设备的宽度?我对此表示怀疑。
-
如果您将其设置为
match_parent@Razgriz,它将永远不会更大
标签: android android-layout android-dialogfragment android-percent-library