Android和布局 [英] Android and Layouts

查看:99
本文介绍了Android和布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到对视图文本:

I need to locate text on view:

文本一些文字应设在底部| center_horizo​​ntal

text 'Some more text' should be located in bottom|center_horizontal

文本短文本应设在与右对齐,但约有10%来自于屏幕上方

text 'Short text' should be located on with align right, but about 10% from the top of the screen

文本XXXX应对准屏幕(第1四中右边/底部对齐)

text 'x.x.x.x' should be aligned to the center of the screen (right/bottom align of the 1st quater)

文本有些长文本..'应放置在顶部/屏幕的3次四中离开,但它应该越过屏幕center_horizo​​ntal。

text 'Some long text ..' should be aligned to the top/left of the 3-rd quater of the screen, but it should cross the center_horizontal of the screen.

推荐答案

下面一对夫妇快速指南:

Here a couple quick guidelines:

  1. 在Android的布局往往是更深层嵌套的比你通常会期望。你经常结束了,只是占用空间空的布局,使其他元素正确布局。
  2. RelativeLayout的是你的朋友,只要你对齐文本到一个特定的优势。
  3. 使用填充设置,把文字有点距离的优势。
  4. 在重力对准了TextView的或按钮中的文本。

再寻找我的图,我复制这种方式:

Looking again I your diagram, I reproduced it this way:

  1. 在开始一个相对布局(fill_content'),占用了整个屏幕。
  2. 把在短文本和一些文本锚定到顶部和底部。
  3. 将带有属性centerInParent零宽度项目在中间的点  屏幕。
  4. 把剩下的由上述项目,并与中心点对齐。
  1. Start with a relative layout ('fill_content') that takes up the entire screen.
  2. Put in the "short text" and "some more text" by anchoring to the top and bottom.
  3. Put a zero-width item with the property "centerInParent" for a point in the middle of the screen.
  4. Put the remaining to items above and aligned with that centerpoint.

不幸的是,没有在第4步工作正常。没有像layout_below工作时,所引用的项目是一个centerInParent项目。相对布局第3步。原来这都和未能fill_content在顶层。是的,布局是棘手的,我希望能有一个调试器他们。

Unfortunately, nothing in step 4 worked correctly. Nothing like "layout_below" worked when the referenced item was a centerInParent item. with relative layouts to step 3. Turns out it had to do with failing to fill_content on the top level. Yes, the layouts are tricky, and I wish there was a debugger for them.

下面是正确的版本:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/r1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
    android:id="@+id/short_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Short Text"
    android:gravity="right"
    android:layout_marginTop="30dip"
    android:layout_alignParentTop="true" />
<TextView
    android:id="@+id/more_text"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Some More Text"
    android:gravity="center"
    android:layout_alignParentBottom="true" />
  <TextView android:id="@+id/centerpoint"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:width="0dip"
    android:height="0dip"
    />
  <TextView android:id="@+id/run_fox"
    android:text="Run, fox, run!"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@id/centerpoint"
    android:layout_toLeftOf="@id/centerpoint" 
    />
<TextView
    android:layout_below="@id/centerpoint"
    android:text="The quick brown fox jumped over the lazy dog, who had been a frog, and then got features and ran slowly."
    android:layout_alignRight="@id/centerpoint"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
 </RelativeLayout>

这篇关于Android和布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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