Android和布局

dav*_*avs 3 android android-layout

我需要在视图上找到文本:

文本'更多文字'应位于bottom | center_horizo​​ntal

文本"短文本"应位于右对齐位置,但距离屏幕顶部约10%

文本'xxxx'应与屏幕中心对齐(第一个四分之一的右/底对齐)

文本'一些长文本..'应该与屏幕的第三个四分之一的顶部/左边对齐,但它应该穿过屏幕的center_horizo​​ntal.

Cha*_*iam 7

这里有几个快速指南:

  1. Android Layouts往往比你通常期望的嵌套得更深.您经常会得到"空"布局,只占用空间,以便其他元素正确布局.
  2. 只要您将文本与特定边缘对齐,RelativeLayout就是您的朋友.
  3. 使用填充设置将文本"稍微远离"边缘.
  4. 重力对齐TextView或按钮中的文本.

再看一下我的图表,我这样再现:

  1. 从占据整个屏幕的相对布局('fill_content')开始.
  2. 通过锚定到顶部和底部来放入"短文本"和"更多文本".
  3. 将具有属性"centerInParent"的零宽度项放在屏幕中间的一个点上.
  4. 将剩下的项目放在上面的项目并与该中心点对齐.

不幸的是,第4步中没有任何工作正常.当引用的项目是centerInParent项时,没有像"layout_below"那样工作.相对布局到第3步.原来它与顶层的fill_content失败有关.是的,布局很棘手,我希望有一个调试器.

这是正确的版本:

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