将 UI 元素的位置/大小设置为屏幕大小的百分比

我试图弄清楚在创建布局时是否可以使用百分比位置/大小。我想要的是这样的东西..。

^
|
|
| 68%
|
|
v
Gallery (height equivalent of 16% total screen size)
^
| 16%
v

我正在测试的设备,在横向有一个显示800x480实际像素,我目前强制它与以下..。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_marginTop ="320px"
/>
</RelativeLayout>

显然,我不想硬编码固定的 px单位,但我不能使用 68%0.68layout_marginTop(例如)。我已经看了 dp单位,但我不知道我是否可以做到这一点,无论是。

我不得不承认 UI 设计是我的一个弱点,所以任何建议都会被感激地接受。

编辑: 为了将来的参考,如果有人正在寻找一个类似的答案,根据艾伦摩尔的建议,我有以下工作正是我想要的..。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/bground"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.68"
android:background="@android:color/transparent"
/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.16"
android:background="@android:color/transparent"
/>
</LinearLayout>

我设法找到了一些使用 layout_weight的其他例子,并决定将 TextView的高度设置为 0dp,还使用浮点数作为权重。干得不错。:)

110391 次浏览

I think what you want is to set the android:layout_weight,

http://developer.android.com/resources/tutorials/views/hello-linearlayout.html

something like this (I'm just putting text views above and below as placeholders):

  <LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="68"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="0dp"


android:layout_weight="16"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="16"/>


</LinearLayout>

Take a look at this:

http://developer.android.com/reference/android/util/DisplayMetrics.html

You can get the heigth of the screen and it's simple math to calculate 68 percent of the screen.

For TextView and it's descendants (e.g., Button) you can get the display size from the WindowManager and then set the TextView height to be some fraction of it:

Button btn = new Button (this);
android.view.Display display = ((android.view.WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
btn.setHeight((int)(display.getHeight()*0.68));

Use the 相对布局百分比 or PercentFrameLayout from the Percent Supoort Library

<android.support.percent.PercentFrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
app:layout_heightPercent="68%"/>
<Gallery
android:id="@+id/gallery"
android:layout_width="match_parent"
app:layout_heightPercent="16%"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</android.support.percent.PercentFrameLayout>

The above problem can also be solved using ConstraintLayout through Guidelines.

Below is the snippet.

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">


<android.support.constraint.Guideline
android:id="@+id/upperGuideLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.68" />


<Gallery
android:id="@+id/gallery"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/lowerGuideLine"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/upperGuideLine" />


<android.support.constraint.Guideline
android:id="@+id/lowerGuideLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.84" />


</android.support.constraint.ConstraintLayout>