安卓和布局设计

3

我需要在视图上定位文本:

文本“Some more text”应位于底部|水平居中

文本“Short text”应右对齐,但距离屏幕顶部约10%

文本“x.x.x.x”应对齐到屏幕的中心(第一象限的右侧/底部对齐)

文本“Some long text ..”应对齐到屏幕的第三象限的左上角,但它应该横跨屏幕的水平中心线。


1
这可能会有所帮助 http://developer.android.com/resources/tutorials/views/hello-relativelayout.html - skyman
你能否制作一个更好的图表? - Charles Merriam
2个回答

7
以下是几点快速指南:
  1. Android布局通常比您预期的更深度嵌套。您经常会遇到“空”布局,这只是为了占用空间,以便其他元素正确布局。
  2. RelativeLayout是您在将文本对齐到特定边缘时的好帮手。
  3. 使用填充设置将文本“稍微远离”边缘。
  4. Gravity将文本对齐在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>

你的意思是我需要使用一组空的TextView,并将它们的layout_width设置为“fill_parent”,并将layout_weight设置为“1”来实现相当可接受的填充效果吗? - davs

0

如果我理解正确,您想在文本视图上简单地放置一系列字符串。您可以在XML中完成这个操作。一个方便的GUI工具是DroidDraw,您可以在浏览器或桌面上运行它。我发现它对于某些GUI事情非常有用。除此之外,您还可以绘制一个视图,就像这样...

   class DrawOnTop extends View { 
    public DrawOnTop(Context context) { 
            super(context); 
            // TODO Auto-generated constructor stub 
    } 
    @Override 
    public void onDraw(Canvas canvas) { 
        Paint paint = new Paint(); 
            paint.setStyle(Paint.Style.FILL); 
            paint.setColor(Color.RED);
            paint.setTextSize(15);
            Paint paint2 = new Paint();
            canvas.drawText("Test", 30, 30, paint);
    } 
} 

在上面的例子中,我定义了一个新的paint。这是为了设置文本的属性。我给它定了一个红色和文本大小15。您也可以添加更多属性。我还绘制了一个字符串“Test”。它位于坐标(30,30),这是我想让Java绘制它的x和y坐标。然后使用了paint的属性。
编辑:此页面也可能会有所帮助http://developer.android.com/reference/android/graphics/Canvas.html

我不应该在特定位置绘制文本。(如果需要,可以使用AbsoluteLayout)。但是我需要我的应用程序支持多个屏幕。 - davs
DroidDraw不支持例如layout_gravity的多个取值,比如说,在XML中我可以使用layout_gravity="bottom|center_horizontal",但是DroidDraw只支持一个取值...也许我误解了什么? - davs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接