2012-04-22 122 views
33

頂我想創建對齊圖像的頂部這樣一個TextView頂部的佈局:對齊的頂部圖像對TextView的

--------- Text text text text text text text 
| Image | text text text text text text text 
--------- text text text text text text text 
      text text text text text text text 
      text text text text text. 

我試圖通過設置android:drawableLeft到我的形象做這個,但垂直居中圖像:

  Text text text text text text text 
--------- text text text text text text text 
| Image | text text text text text text text 
--------- text text text text text text text 
      text text text text text. 

這是可能的只是一個TextView還是我需要創建一個包含一個TextView和ImageView的一個RelativeLayout的?

這裏是我的TextView的XML佈局,讓不正確的佈局:

<TextView 
    android:gravity="top" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:drawableLeft="@drawable/image" 
    android:drawablePadding="8dp" 
    android:text="@string/text" /> 

android:gravity="top"屬性似乎隻影響文本,而不是繪製。

+2

我認爲你需要使用的RelativeLayout ... – amp 2012-04-22 11:01:56

+0

你是否需要在頂部或左側 – 2012-04-22 11:03:03

+0

作爲@amp說使用相對佈局最好.. – MAC 2012-04-22 12:25:09

回答

1

嘗試使用RelativeLayout的......

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/imageView1" 
     android:text="@string/text" /> 

</RelativeLayout> 
+0

CompoundDrawable的要點是儘量減少View層次結構中的視圖數量。儘管這個解決方案在外觀上看起來相同,但它並不回答這個問題。 – rds 2016-06-13 13:21:10

0
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imageView1" 
     android:text="@string/text" /> 

</RelativeLayout> 

這將做到這一點。

1

我認爲,有比3視圖更容易和更快的方式。您需要爲圖標準備適當的9patch,然後使用FrameLayout。甚至有可能只有單一EditText/TextView使用相同的可繪製背景。更多細節在this answer中描述。

0

使用layout_alignTop。有了它,你可以對齊視圖頂部到另一個視圖頂部

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/text" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignTop="@id/textView1" 
     android:layout_toLeftOf="@id/textView1" 
     android:src="@drawable/ic_launcher" />  

</RelativeLayout> 
22

您可以通過創建一個包裝您可繪製自定義繪製對象對準複合抽拉頂端(或底部),然後操縱通過重寫方法onDraw(Canvas)來繪製您的自定義Drawable。

下面的示例是最簡單的示例。這將圖像對齊到頂部,但您也可以通過在onDraw(Canvas)-方法中實現所需的邏輯,使其與TextView的底部,左側或右側對齊。您可能還想在onDraw(Canvas)中創建邊距,以使您的設計實現像素完美。

使用範例:

GravityCompoundDrawable gravityDrawable = new GravityCompoundDrawable(innerDrawable); 
// NOTE: next 2 lines are important! 
innerDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight()); 
gravityDrawable.setBounds(0, 0, innerDrawable.getIntrinsicWidth(), innerDrawable.getIntrinsicHeight()); 
mTextView.setCompoundDrawables(gravityDrawable, null, null, null); 

示例代碼:

public class GravityCompoundDrawable extends Drawable { 

    // inner Drawable 
    private final Drawable mDrawable; 

    public GravityCompoundDrawable(Drawable drawable) { 
     mDrawable = drawable; 
    } 

    @Override 
    public int getIntrinsicWidth() { 
     return mDrawable.getIntrinsicWidth(); 
    } 

    @Override 
    public int getIntrinsicHeight() { 
     return mDrawable.getIntrinsicHeight(); 
    } 

    @Override 
    public void draw(Canvas canvas) { 
     int halfCanvas= canvas.getHeight()/2; 
     int halfDrawable = mDrawable.getIntrinsicHeight()/2; 

     // align to top 
     canvas.save(); 
     canvas.translate(0, -halfCanvas + halfDrawable); 
     mDrawable.draw(canvas); 
     canvas.restore(); 
    } 
} 
+0

偉大的解決方案。這必須是被接受的答案。 – TharakaNirmana 2015-07-21 09:18:22

+0

工作..謝謝 – 2015-12-16 09:18:20

+1

如何用它來對齊文本和左邊的drawable到textview的左邊 – TheReprator 2016-03-02 05:16:05