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

Squ*_*onk 69 android android-layout

我正在努力研究在创建布局时是否可以使用百分比位置/大小.我想要的是这样的......

^
|
|
| 68%
|
|
v
Gallery (height equivalent of 16% total screen size)
^
| 16%
v
Run Code Online (Sandbox Code Playgroud)

我正在测试一个设备,它在横向上有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>
Run Code Online (Sandbox Code Playgroud)

很显然,我不想硬编码固定px装置,但我不能使用68%0.68用于layout_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>
Run Code Online (Sandbox Code Playgroud)

我设法找到了一些其他的使用示例,layout_weight并决定将TextView高度设置为0dp并且还使用浮动来表示权重.工作得很好.:)

Ala*_*ore 68

我想你想要的是设置android:layout_weight,

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

像这样的东西(我只是将文本视图放在上面和下面作为占位符):

  <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>
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案,但请注意,如果将 LinearLayout 的 android:layout_weight="1" 替换为 android:weightSum="100" 并将所有子项的高度设置为 0dp,则可以删除 TextViews 之一。这是因为在这种情况下,在 LinearLayout 上设置 android:layout_weight 无关紧要。如果您设置 android:weightSum 的 LinearLayout 所有未使用的权重将保持为空(正如预期的那样)。 (2认同)

rah*_*lrv 17

使用 PercentRelativeLayoutPercentFrameLayout百分比Supoort库

<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>
Run Code Online (Sandbox Code Playgroud)

  • 这两种布局都已在Android API级别26.1.0中折旧 (6认同)
  • 天哪,太棒了。为什么要花这么长时间添加它们。谢谢!你让我今天很开心。:) (2认同)
  • 有点混乱,您需要一个自定义布局来执行此操作,它应该在内置布局中可用...... (2认同)

jos*_*ris 16

对于TextView及其后代(例如Button),您可以从WindowManager获取显示大小,然后将TextView高度设置为其中的一小部分:

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


Ujw*_*wal 6

通过ConstraintLayout通过Guide,也可以解决上述问题。

以下是代码段。

<?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>
Run Code Online (Sandbox Code Playgroud)