2016-09-10 15 views
0

Ich weiß, das ist eine beliebte Frage, aber alle Antworten, die ich gefunden habe, funktionieren entweder nicht oder brechen mein Layout. Ich schreibe das Layout für ein Listenansichtselement, es sollte einfach eine Textansicht auf der linken Seite und ein Bild auf der rechten Seite sein, kein Problem damit. Ich habe versucht, Android: layout_gravity = "center_horizontal", aber das ändert nichts. Ich habe auch versucht, mein LinearLayout in ein RelativeLayout zu ändern und layout_centerVertical = "true" zu verwenden, das horizontal zentriert ist, aber Bilder in Zeilen mit unterschiedlichen Höhen sind etwas größer als andere. Das ist mein Layout-Code:Vertikale Zentrierung Bildansicht in Relative Layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="70dp" 
    android:paddingLeft="8dp" 
    android:paddingRight="8dp" 
    android:background="?android:attr/selectableItemBackground"> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="3"> 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_weight="2" 
      android:paddingTop="8dp" 
      android:paddingLeft="8dp" 
      android:paddingRight="8dp"> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textSize="18dp" 
       android:textColor="#000000" 
       android:text="Titolo di prova lorem ipsus dolor sit amet..." 
       android:id="@+id/article_title"/> 
      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Autore di prova" 
       android:id="@+id/article_info"/> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:visibility="invisible" 
       android:text="non dovresti poter vedere questa scritta..." 
       android:id="@+id/article_hiddenid"/> 
     </LinearLayout> 
<!-- Here's where it should be centered --> 
     <LinearLayout 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:padding="2dp" 
      android:orientation="vertical"> 
       <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="70dp" 
        android:src="@drawable/demoimage2" 
        android:layout_gravity="center_horizontal" 
        android:id="@+id/article_image"/> 
     </LinearLayout> 
    </LinearLayout> 
</LinearLayout> 

edit: Hier ist, wie es aussieht imgur.com/a/3ZGrl. Wenn ich RelativeLayout und layout_centerVertical verwende, sieht das so aus: imgur.com/a/vWrzH (einige Bilder sind etwas breiter als andere)

Antwort

0

Sie verschachteln zu viele LinearLayout ohne Ursache.

Versuchen Sie zunächst, die Ausrichtung für das übergeordnete LinearLayout auf horizontal zu setzen. Und entfernen Sie das zweite verschachtelte Layout. Versuchen Sie folgendes:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:paddingLeft="8dp" 
android:paddingRight="8dp" 
android:weightSum="3" 
android:orientation="horizontal" 
android:background="?android:attr/selectableItemBackground"> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:layout_weight="2" 
     android:paddingTop="8dp" 
     android:paddingLeft="8dp" 
     android:paddingRight="8dp"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="18dp" 
      android:textColor="#000000" 
      android:text="Titolo di prova lorem ipsus dolor sit amet..." 
      android:id="@+id/article_title"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Autore di prova" 
      android:id="@+id/article_info"/> 

    </LinearLayout> 

    <LinearLayout 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:padding="2dp" 
     android:orientation="vertical"> 
      <ImageView 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:src="@drawable/demoimage2" 
       android:id="@+id/article_image"/> 
    </LinearLayout> 
</LinearLayout> 
+0

Vielen Dank für die Bereinigung aber nichts geändert, es ist das gleiche wie vor –

+0

Könnten Sie bitte ein Bild teilen, was die Ausgabe ist und was erwarten Sie –

+0

http://imgur.com/a/ 3ZGrl so sieht es auf meinem Handy aus. Sie können sehen, dass die Bilder auf der rechten Seite nicht vertikal auf die Nachrichten zentriert sind, die mehr einen Titel länger als 3 Zeilen haben. Wenn ich RelativeLayout und layout_centerVertical verwende, sind die Bilder zentriert, aber in den Nachrichten mit mehr als 3 Zeilen Titel sind sie etwas größer. Es ist nicht viel, aber es ist komisch http://imgur.com/a/vWrzH –

Verwandte Themen