简体   繁体   English

无法在Android中的表格单元格中对齐ImageView

[英]Can't Align ImageView in Table Cell in Android

OK, here's ANOTHER question about positioning objects in tables in Android, but nothing I've found on Stack has helped. 好的,这是另一个有关在Android中的表中放置对象的问题,但是我在Stack上找不到的任何帮助。

I want the ImageView in the XML below to be left aligned in its cell. 我希望下面的XML中的ImageView在其单元格中保持对齐。 No matter what I've tried, though, it won't do this (it's always centered). 但是,无论我尝试了什么,它都不会这样做(它始终居中)。 The layout_width and layout_weight are set as they are to control the position and sizes of the cells themselves. 按原样设置layout_widthlayout_weight来控制单元格本身的位置和大小。 Maybe these settings are making it impossible to left align the ImageView in its cell, though. 但是,也许这些设置使ImageView无法在其单元格中左对齐。

android:layout_gravity="left" and android:gravity="left" didn't work in either the ImageView tag or the TableRow tag. android:layout_gravity="left"android:gravity="left"在ImageView标记或TableRow标记中均无效。 One kind of kludgy solution that seems to work is to add a blank TextView in a third column, to force the 2nd column holding the ImageView to be exactly the width of the ImageView 's bitmap. 一种可行的解决方案是在第三列中添加一个空白TextView,以强制包含ImageView的第二列恰好是ImageView位图的宽度。 However, I'd prefer not to have to do this, especially since different rows may contain bitmaps of different widths. 但是,我不想这样做,尤其是因为不同的行可能包含不同宽度的位图。

<TableLayout
     android:id="@+id/scoreTable"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_alignParentBottom="true"
     android:layout_alignParentLeft="true" >

     <TableRow
         android:id="@+id/thisWeek"
         android:layout_width="wrap_content"
         android:layout_height="fill_parent" >

         <TextView
             android:id="@+id/thisWeekText"
             android:layout_width="0px"
             android:layout_height="30dp"
             android:layout_weight="35"
             android:gravity="right"
             android:paddingRight="30px"
             android:text="@string/thisWeek"
             android:textStyle="bold" />


         <ImageView
             android:id="@+id/chevron1"
             android:layout_width="0px"
             android:layout_height="20dp"
             android:layout_weight="65"
             android:layout_marginLeft="0px"
             android:src="@drawable/doser_scale4a"
             android:visibility="visible" />


     </TableRow>

I replaced the TextViews with ImageViews, using bitmaps of the text that was in the TextViews. 我使用TextViews中的文本位图将ImageViews替换为ImageViews。 This was OK for me because that text won't be changing, though I guess if it did I could create images for all possible text and switch between those images. 这对我来说是可以的,因为该文本不会更改,尽管我猜是否可以为所有可能的文本创建图像并在这些图像之间切换。

This was not an ideal solution, since I assume using bitmaps instead of text uses more memory, and making bitmaps for my text required a little time and effort and some sacrifice of ease and flexibility of changing the text, if that is desired at some point, but I couldn't figure out how to make this work with TextViews. 这不是一个理想的解决方案,因为我假设使用位图而不是文本会占用更多的内存,并且为我的文本制作位图需要花费一些时间和精力,并且在某些时候需要更改文本的便利性和灵活性,这是需要的,但我不知道如何使用TextViews进行这项工作。 Android definitely seems limited in this way, since it shouldn't be that difficult to align text any way you want in a table cell, should it? Android绝对似乎以这种方式受到限制,因为以所需的方式在表格单元格中对齐文本并不难,不是吗?

TableRow的每个“单元”都是一个View,因此您可以放置​​LinearLayout(具有首选的重力)并将其放置在LinearLayour中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM