簡體   English   中英

Android和布局

[英]Android and Layouts

我需要在視圖上找到文本:

文本'更多文字'應位於bottom | center_horizo​​ntal

文本“短文本”應位於右對齊位置,但距離屏幕頂部約10%

文本'xxxx'應與屏幕中心對齊(第一個四分之一的右/底對齊)

文本'一些長文本..'應該與屏幕的第三個四分之一的頂部/左邊對齊,但它應該穿過屏幕的center_horizo​​ntal。

這里有幾個快速指南:

  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>

如果我理解正確,你只想在文本視圖上添加一些字符串。 您可以在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);
    } 
} 

在上面的例子中,我定義了一個新的顏料。 這是為了設置文本的屬性。 我給它一個紅色和文本大小15.你也可以添加更多的屬性。 我還畫了一個字符串“Test”。 在坐標(30,30)處,這些是我希望Java繪制它的x和y坐標。 然后使用paint的屬性。

編輯:此頁面也可能有所幫助http://developer.android.com/reference/android/graphics/Canvas.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM