2017-08-08 49 views
0

我只是想在我的TextView的頂部和左上角設置圖標。 這是我的代碼和輸出分別爲:如何將android:drawable設置爲left和top?

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Content" 
    android:drawableLeft="@drawable/icon" /> 

輸出:

outPut

,但我想我的圖標設置爲頂部和左側像這樣的:

enter image description here

+0

它不可能輕易與單一視圖做到這一點,你可能需要使用的RelativeLayout和圖像的單獨ImageView的。 –

+0

使用引力並設置它 –

+0

@ jigarsavaliya你能解釋更多嗎? –

回答

3

爲此,你必須採取單獨的ImageView這樣在TextView的領域,你必須添加一行代碼,機器人:layout_toRightOf =「@ + ID/ImageView的」: 爲了更好地理解看下面的代碼:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 
    <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/imageView" 
     android:background="@mipmap/ic_launcher" 
     /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@+id/imageView" 
     android:text="this si fodfjkhsdhaffjsdfasdfjhsdfhjsdhfjhsdfhsdhfhdsjfhsdjhfjhdsfhsdhfjhsdjhfjsdhfjhsdjfhjsdhfjhsdjfhjdshfsdjhfjsdhfsdkjhfjsdhfjhsdjfhjsdhjfhsdjhfjsdhfjhjsdhfjsdhjfhsdjf" 
     /> 

</RelativeLayout> 
+0

完美的答案.. –

+0

thnx @komalakhani –

+0

感謝您的反饋@ DeepakRana –

0

使用;

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Content" 
    android:drawableTop="@drawable/def" 
    android:drawableLeft="@drawable/icon" /> 
0

試試這個

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

<TextView 
    android:id="@+id/tv1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="hello world" 
    android:layout_toEndOf="@+id/imageView" /> 



<ImageView 
    android:id="@+id/imageView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_error" /> 


</RelativeLayout> 
+0

嵌套佈局不利於演出。改爲使用RelativeLayout。或者一個GridView,一個TableLayout,......任何事情都可以讓你不將LinearLayouts嵌套在另一箇中。 –

+0

@ModularSynth檢查更新ans –

+0

我會把ImageView(它實際上是一個)之前。然後把TexView相對於它。但是,你會得到@ deepakrana的答案。 –

0

您可以對齊複合抽拉頂端(或底部)通過創建一個自定義Drawable來包裝Drawable。

使用

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()); 
textView.setCompoundDrawables(gravityDrawable, null, null, null); 

定製GravityCompoundDrawable類:

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

此代碼將幫助您和見附件圖像。

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

    <LinearLayout 
     android:id="@+id/llMain" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="1"> 

     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.10" 
      android:orientation="vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@mipmap/ic_launcher" /> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@mipmap/ic_launcher" /> 

     </LinearLayout> 

     <TextView 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="0.90" 
      android:text="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using" /> 
    </LinearLayout> 
</LinearLayout> 

https://i.stack.imgur.com/hLUGT.png

+0

哦,不...再次!表演嵌套佈局不利。改爲使用RelativeLayout。或者一個GridView,一個TableLayout,...無論如何都不允許將LinearLayouts嵌套在另一箇中。 –

相關問題