2015-03-03 80 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" />