UI元素的设定位置/大小屏幕尺寸的百分比 [英] Set position / size of UI element as percentage of screen size

查看:155
本文介绍了UI元素的设定位置/大小屏幕尺寸的百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出是否创建布局时可能使用的百分比位置/尺寸。我想是这样的事情...

I'm trying to work out if it's possible to use percentage positions/sizes when creating a layout. What I want is something like this...

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

我测试其景观具有800×480的实际像素的显示设备上和我目前正在迫使它具有以下...

I'm testing on a device which in landscape has a display of 800x480 actual pixels and I'm currently forcing it with the following...

<?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>

很显然,我不想硬code固定 PX 单元,但我不能使用 68% 0.68 layout_marginTop (例如)。我看 DP 单元,但我不知道如果我能做到这一点无论是那种方式。

Obviously I don't want to hard-code fixed px units but I can't use 68% or 0.68 for layout_marginTop (for example). I've looked at dp units but I'm not sure if I can do it that way either.

我不得不承认,UI设计是我的弱点,所以任何建议将受到欢迎。

I have to admit UI design is a weak point of mine so any advice would be gratefully received.

编辑:为了未来的参考,如果有人正在寻找类似的回答,下面的艾伦·摩尔的建议,我有以下的工作究竟如何我希望它...

For future reference if anyone is looking for a similar answer, following Alan Moore's suggestion I have the following working exactly how I want it...

<?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 ,也可用于浮子的权重。伟大的工作。 :)

I managed to find some other examples of using layout_weight and decided to set the TextView heights to 0dp and also used floats for the weights. Working great. :)

推荐答案

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

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

<一个href="http://developer.android.com/resources/tutorials/views/hello-linearlayout.html">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:layout_weight="1">
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="68"/>
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"

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

  </LinearLayout>

这篇关于UI元素的设定位置/大小屏幕尺寸的百分比的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆