33

Chcę stworzyć układ, który wyrównuje górną część obrazu na szczycie TextView jak ten:Align top obrazu do góry 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. 

Próbowałem robić to poprzez ustawienie android:drawableLeft do mojego obrazu , ale że centra obraz w pionie:

  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. 

jest to możliwe tylko z TextView czy muszę utworzyć RelativeLayout zawierający TextView oraz ImageView?

Oto układ XML dla mojego TextView który daje nieprawidłowy układ:

<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" /> 

Atrybut android:gravity="top" tylko wydaje się wpływać na tekst, a nie rozciągliwej.

+2

Myślę, że trzeba użyć relativeLayout ... – amp

+0

czy trzeba w górę lub w lewo –

+0

jak @amp mówi używać względna layout Najlepiej .. – MAC

Odpowiedz

1

spróbuje użyć 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

Punkt CompoundDrawable to minimalizacja liczby widoków w hierarchii widoku. To rozwiązanie nie odpowiada na pytanie, mimo że będzie wyglądać tak samo. – rds

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> 

by to zrobić.

1

Myślę, że jest łatwiejszy i szybszy sposób niż 3 widoki. Musisz przygotować odpowiednią 9-stronicową ikonę, a następnie użyć FrameLayout. Jest nawet możliwe, że tylko/używają tego samego losowania co ich tło. Więcej szczegółów opisano w this answer.

0

Użyj layout_alignTop. Dzięki nim można wyrównać górnej części widoku na szczyt innego widoku

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

można wyrównać bezkonsolowe rozciągliwej na górę (lub w dół), tworząc niestandardowy rozciągliwej, która otacza swoją rozciągliwej, a następnie manipulować rysunek twojego zwyczaju Drawable przez przesłonięcie metody onDraw(Canvas).

Poniższy przykład to najprostszy możliwy przykład. Wyrównuje to obraz do góry, ale można go również dopasować do dolnej, lewej lub prawej strony TextView, wprowadzając wymaganą logikę w metodzie onDraw(Canvas). Możesz także chcieć zbudować margines w modelu onDraw(Canvas), aby uzyskać perfekcyjną implementację projektu.

wykorzystanie próbki:

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

Przykładowy kod:

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

świetne rozwiązanie. To musi być zaakceptowana odpowiedź. – TharakaNirmana

+0

pracował ..dziękuję –

+1

jak użyć tego do wyrównania tekstu i lewej strony do pobrania na lewo od widoku tekstowego – TheReprator