2015-03-03 96 views
0

我的ImageViewTextView未正確對齊。是否因爲我的佈局設置?我想要的是ImageView將父對象左對齊和TextView對齊。Android - ImageView在TableLayout中未正確對齊

enter image description here

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

    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ffffff" 
      android:layout_marginTop="10dp" 
      android:padding="10dp" > 

      <ImageView 
       android:id="@+id/profilePicture" 
       android:layout_width="80dp" 
       android:layout_height="80dp" 
       android:layout_marginRight="8dp" 
       android:src="@drawable/user_50" 
       android:gravity="center" ></ImageView> 

      <TextView 
         android:id="@+id/textView1" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:text="Mohammad Nurdin, 26" 
         android:gravity="center"/> 


     </TableRow> 

<!-- .. --> 

</TableLayout> 
</ScrollView> 

請指點如何解決它。

+0

使用layout_gravity = 「左」 用於IV和layout_gravity = 「右」 用於電視,而不是重力和檢查。請讓我知道它是否有效 – shivamDev 2015-03-03 14:10:39

+0

您可以在表格行內使用相對佈局,然後將父級設置爲左側或您想要的任何內容。 – 2015-03-03 14:11:12

+0

它不起作用 – 2015-03-03 14:11:33

回答

0

我已經找到了答案。在TableLayout內使用LinearLayout

<TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TableRow 
      android:id="@+id/tableRow1" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#ffffff" 
      android:layout_marginTop="10dp" 
      android:padding="10dp" > 

      <LinearLayout android:orientation="horizontal" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

      <ImageView 
       android:layout_weight="1" 
       android:id="@+id/profilePicture" 
       android:layout_width="80dp" 
       android:layout_height="80dp" 
       android:src="@drawable/user_50" 
       android:gravity="left" ></ImageView> 

      <TextView 
         android:layout_weight="100" 
         android:id="@+id/textView1" 
         android:layout_width="0dip" 
         android:layout_height="wrap_content" 
         android:text="Mohammad Nurdin, 26"/> 

      </LinearLayout> 
     </TableRow> 

<!-- ? --> 

</TableLayout> 
-1

這是因爲重力

android:gravity="center" 

的你可以把它變成

android:gravity="left" 
+0

這不是我的朋友.. – 2015-03-03 14:05:18

0

我想是這樣的,它是根據您的要求至少

<TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#ffffff" 
     android:layout_marginTop="10dp" 
     android:padding="10dp" > 

     <ImageView 
      android:id="@+id/profilePicture" 
      android:layout_width="300dp" 
      android:layout_height="80dp" 
      android:layout_marginRight="8dp" 
      android:src="@drawable/user_50" 
      android:gravity="center" 
      android:layout_gravity="left"></ImageView> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="left" 
      android:paddingBottom="10dp" 
      android:paddingRight="10dp" 
      android:paddingTop="10dp" 
      android:text="Mohammad Nurdin, 26" 
      android:textAppearance="?android:attr/textAppearanceMedium" 
      android:layout_column="70" 
      android:layout_gravity="center" /> 

    </TableRow> 

希望它幫助... :)

0

添加到您的TableLayout:android:stretchColumns="0,1"這是可選的的TableRow android:gravity="center"

<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:stretchColumns="0,1"> 

    <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="10dp" 
     android:gravity="center" 
     android:background="#ffffff" 
     android:padding="5dp"> 

     <ImageView 
      android:id="@+id/profilePicture" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:src="@drawable/user" /> 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Mohammad Nurdin, 26" /> 


    </TableRow> 

</TableLayout> 

輸出是這樣的:item layout

0

重力不會對ImageViews在TableLayout像它的TextViews工作,你需要做的是包裹ImageView的一個RelativeLayout的內部,然後對齊的ImageView到其新的父..所以這樣的..

<TableRow 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="4"> 

     <ImageView 
      android:id="@+id/myImage" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:src="@mipmap/ic_launcher" /> 
    </RelativeLayout> 

    <TextView 
     android:id="@+id/myText" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="6" 
     android:gravity="left" 
     android:text="hello" />