简体   繁体   English

Android和布局

[英]Android and Layouts

I need to locate text on view: 我需要在视图上找到文本:

text 'Some more text' should be located in bottom|center_horizontal 文本'更多文字'应位于bottom | center_horizo​​ntal

text 'Short text' should be located on with align right, but about 10% from the top of the screen 文本“短文本”应位于右对齐位置,但距离屏幕顶部约10%

text 'xxxx' should be aligned to the center of the screen (right/bottom align of the 1st quater) 文本'xxxx'应与屏幕中心对齐(第一个四分之一的右/底对齐)

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. 文本'一些长文本..'应该与屏幕的第三个四分之一的顶部/左边对齐,但它应该穿过屏幕的center_horizo​​ntal。

Here a couple quick guidelines: 这里有几个快速指南:

  1. Android Layouts tend to be much more deeply nested than you would normally expect. Android Layouts往往比你通常期望的嵌套得更深。 You often end up with "empty" layouts that just take up space so that other elements lay out correctly. 您经常会得到“空”布局,只占用空间,以便其他元素正确布局。
  2. RelativeLayout is your friend whenever you are aligning text to a particular edge. 只要您将文本与特定边缘对齐,RelativeLayout就是您的朋友。
  3. Use the padding settings to put text "a little away from" an edge. 使用填充设置将文本“稍微远离”边缘。
  4. Gravity aligns the text within that TextView or button. 重力对齐TextView或按钮中的文本。

Looking again I your diagram, I reproduced it this way: 再看一下我的图表,我这样再现:

  1. Start with a relative layout ('fill_content') that takes up the entire screen. 从占据整个屏幕的相对布局('fill_content')开始。
  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. 将具有属性“centerInParent”的零宽度项放在屏幕中间的一个点上。
  4. Put the remaining to items above and aligned with that centerpoint. 将剩下的项目放在上面的项目并与该中心点对齐。

Unfortunately, nothing in step 4 worked correctly. 不幸的是,第4步中没有任何工作正常。 Nothing like "layout_below" worked when the referenced item was a centerInParent item. 当引用的项目是centerInParent项时,没有像“layout_below”那样工作。 with relative layouts to step 3. Turns out it had to do with failing to fill_content on the top level. 相对布局到第3步。原来它与顶层的fill_content失败有关。 Yes, the layouts are tricky, and I wish there was a debugger for them. 是的,布局很棘手,我希望有一个调试器。

Here's the correct version: 这是正确的版本:

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

If I understand correctly you want to simply put a serious of strings on the text view. 如果我理解正确,你只想在文本视图上添加一些字符串。 You can do this in XML. 您可以在XML中执行此操作。 A handy GUI tool for this would be DroidDraw You can run it in a browser or on your Desktop. 一个方便的GUI工具是DroidDraw您可以在浏览器或桌面上运行它。 I find it handy for some GUI things. 我发现一些GUI的东西很方便。 Other than that you can Draw a view, something like this... 除此之外你可以画一个视图,像这样......

   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);
    } 
} 

In the above example I've defined a new paint. 在上面的例子中,我定义了一个新的颜料。 This is to set the properties of the text. 这是为了设置文本的属性。 I've given it a red colour and text size 15. You can add more attributes too. 我给它一个红色和文本大小15.你也可以添加更多的属性。 I've also drawn a string "Test". 我还画了一个字符串“Test”。 It is at coordinates (30,30) these are the x and y coordinates where I want Java to draw it. 在坐标(30,30)处,这些是我希望Java绘制它的x和y坐标。 It's then using paint's attributes. 然后使用paint的属性。

EDIT: this page may also be of some help http://developer.android.com/reference/android/graphics/Canvas.html 编辑:此页面也可能有所帮助http://developer.android.com/reference/android/graphics/Canvas.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM