Android 自定义Listview奇怪的UI故障
我正在努力调试android应用程序中ListView的一个奇怪的UI故障。99%的时间里,所有的东西看起来和工作起来都像它应该的那样,但我的ListView有时表现得很奇怪。重新启动应用程序时,listview看起来又正常了 有人知道这是否是一个已知的安卓bug吗 这只是随机发生的事实(我试图找出一个模式,不能)让我害怕。我还没有在网上找到任何关于类似问题的信息。希望我只是在谷歌上搜索了错误的搜索词 如有任何建议/帮助,将不胜感激 提前谢谢 列表视图通常的样子:Android 自定义Listview奇怪的UI故障,android,android-layout,android-listview,Android,Android Layout,Android Listview,我正在努力调试android应用程序中ListView的一个奇怪的UI故障。99%的时间里,所有的东西看起来和工作起来都像它应该的那样,但我的ListView有时表现得很奇怪。重新启动应用程序时,listview看起来又正常了 有人知道这是否是一个已知的安卓bug吗 这只是随机发生的事实(我试图找出一个模式,不能)让我害怕。我还没有在网上找到任何关于类似问题的信息。希望我只是在谷歌上搜索了错误的搜索词 如有任何建议/帮助,将不胜感激 提前谢谢 列表视图通常的样子: <RelativeLay
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_height_padded"
android:descendantFocusability="blocksDescendants"
android:layout_margin="0dp"
android:padding="@dimen/padding_list">
<!--This is the clickable background of the item-->
<ImageView
android:id="@+id/imageview_poi_tile_detail_button_detail"
android:background="@color/ca"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_toLeftOf="@+id/imageview_poi_tile_map_button"/>
<!--This is the Grey vertical line next to the icon on the left-->
<ImageView
android:id="@+id/delimiter_poi_tile"
android:layout_width="@dimen/delimiter_size"
android:layout_height="@dimen/tile_description_height"
android:layout_marginTop="@dimen/margin_list_vertical"
android:layout_marginBottom="@dimen/margin_list_vertical"
android:background="@color/git"
android:layout_toRightOf="@id/imageview_poi_tile_icon"/>
<!--This is the red map button on the right-->
<ImageView
android:id="@id/imageview_poi_tile_map_button"
android:background="@color/lr"
android:src="@drawable/map"
android:scaleType="fitCenter"
android:padding="@dimen/image_button_padding"
android:layout_width="@dimen/button_size"
android:layout_height="match_parent"
android:layout_alignParentRight="true"/>
<!--This is the marker Icon on the left-->
<ImageView
android:id="@+id/imageview_poi_tile_icon"
android:src="@drawable/poidefaultgit"
android:scaleType="fitStart"
android:padding="@dimen/image_button_padding"
android:background="@color/ca"
android:layout_width="@dimen/button_size"
android:layout_height="@dimen/tile_description_height"
android:layout_margin="0dp"/>
<!--This is the bold title text, eg. BARONS-->
<TextView
android:id="@+id/textview_poi_tile_type"
android:background="@color/ca"
android:paddingLeft="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="Poi Type"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_title_height"
android:layout_toRightOf="@id/delimiter_poi_tile"
android:layout_toLeftOf="@+id/textview_poi_tile_distance"/>
<!--This is the address that is shown, eg 3 ADDERLEY ST, CAPE TOWN,-->
<TextView
android:id="@+id/textview_poi_tile_description"
android:background="@color/ca"
android:paddingLeft="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textview_poi_tile_type"
android:layout_toRightOf="@id/delimiter_poi_tile"
android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/>
<!--This will display a string when the gps is on, not shown in image as gps was off in screenshot-->
<TextView
android:id="@id/textview_poi_tile_distance"
android:background="@color/ca"
android:textColor="@color/lr"
android:text=""
android:paddingRight="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/>
列表视图时不时的样子:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_height_padded"
android:descendantFocusability="blocksDescendants"
android:layout_margin="0dp"
android:padding="@dimen/padding_list">
<!--This is the clickable background of the item-->
<ImageView
android:id="@+id/imageview_poi_tile_detail_button_detail"
android:background="@color/ca"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:layout_toLeftOf="@+id/imageview_poi_tile_map_button"/>
<!--This is the Grey vertical line next to the icon on the left-->
<ImageView
android:id="@+id/delimiter_poi_tile"
android:layout_width="@dimen/delimiter_size"
android:layout_height="@dimen/tile_description_height"
android:layout_marginTop="@dimen/margin_list_vertical"
android:layout_marginBottom="@dimen/margin_list_vertical"
android:background="@color/git"
android:layout_toRightOf="@id/imageview_poi_tile_icon"/>
<!--This is the red map button on the right-->
<ImageView
android:id="@id/imageview_poi_tile_map_button"
android:background="@color/lr"
android:src="@drawable/map"
android:scaleType="fitCenter"
android:padding="@dimen/image_button_padding"
android:layout_width="@dimen/button_size"
android:layout_height="match_parent"
android:layout_alignParentRight="true"/>
<!--This is the marker Icon on the left-->
<ImageView
android:id="@+id/imageview_poi_tile_icon"
android:src="@drawable/poidefaultgit"
android:scaleType="fitStart"
android:padding="@dimen/image_button_padding"
android:background="@color/ca"
android:layout_width="@dimen/button_size"
android:layout_height="@dimen/tile_description_height"
android:layout_margin="0dp"/>
<!--This is the bold title text, eg. BARONS-->
<TextView
android:id="@+id/textview_poi_tile_type"
android:background="@color/ca"
android:paddingLeft="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="Poi Type"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="match_parent"
android:layout_height="@dimen/tile_title_height"
android:layout_toRightOf="@id/delimiter_poi_tile"
android:layout_toLeftOf="@+id/textview_poi_tile_distance"/>
<!--This is the address that is shown, eg 3 ADDERLEY ST, CAPE TOWN,-->
<TextView
android:id="@+id/textview_poi_tile_description"
android:background="@color/ca"
android:paddingLeft="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/textview_poi_tile_type"
android:layout_toRightOf="@id/delimiter_poi_tile"
android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/>
<!--This will display a string when the gps is on, not shown in image as gps was off in screenshot-->
<TextView
android:id="@id/textview_poi_tile_distance"
android:background="@color/ca"
android:textColor="@color/lr"
android:text=""
android:paddingRight="@dimen/padding_list"
android:layout_margin="0dp"
android:gravity="left|top"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/imageview_poi_tile_map_button"/>
尝试每次膨胀一个新视图,而不是重复使用它们。我知道这不是最好的办法,但在某个时候我也遇到过类似的问题,这是唯一有效的办法。我认为有几种原因导致了这种情况。根据经验,我注意到RelativeLayout有时会根据可用空间不适当地调整其孩子的大小。如果一个孩子的一侧没有适当地固定在某个地方,它会很快让东西被剪掉。此外,我认为文本视图的一些大小不足以显示文本。我在这里做了下面的工作,所以它可能需要一些调整,但是它应该画出布局的图片来尝试。不幸的是,它没有您当前的XML那么简单,但它有望解决您的问题。抱歉,当前没有在计算机上测试此项
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!--This is the marker Icon on the left-->
<!--Use margin if you need more space to divider-->
<ImageView
android:id="@+id/imageview_poi_tile_icon"
android:src="@drawable/poidefaultgit"
android:scaleType="fitStart"
android:padding="@dimen/image_button_padding"
android:background="@color/ca"
android:layout_width="@dimen/button_size"
android:layout_height="match_parent"/>
<!--This is the Grey vertical line next to the icon on the left-->
<!--Use margin to determine how close the line is to the top/bottom of item-->
<ImageView
android:id="@+id/delimiter_poi_tile"
android:layout_width="@dimen/delimiter_size"
android:layout_height="match_parent"
android:layout_marginTop="@dimen/margin_list_vertical"
android:layout_marginBottom="@dimen/margin_list_vertical"
android:src="@color/git"
android:background="@color/ca"/>
<RelativeLayout
android:gravity="center_vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@color/ca">
<!--This is the bold title text, eg. BARONS-->
<TextView
android:id="@+id/textview_poi_tile_type"
android:paddingLeft="@dimen/padding_list"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold"
android:text="Poi Type"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!--This is the address that is shown, eg 3 ADDERLEY ST, CAPE TOWN,-->
<TextView
android:id="@+id/textview_poi_tile_description"
android:paddingLeft="@dimen/padding_list"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Address"
android:ellipsize="end"
android:maxLines="1"
android:textColor="@color/git"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textview_poi_tile_type"/>
<!--This will display a string when the gps is on, not shown in image as gps was off in screenshot-->
<TextView
android:id="@id/textview_poi_tile_distance"
android:textColor="@color/lr"
android:text=""
android:paddingRight="@dimen/padding_list"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/textview_poi_tile_type"/>
</RelativeLayout>
<!--This is the red map button on the right-->
<ImageButton
android:id="@id/imageview_poi_tile_map_button"
android:background="@color/lr"
android:src="@drawable/map"
android:scaleType="fitCenter"
android:padding="@dimen/image_button_padding"
android:layout_width="@dimen/button_size"
android:layout_height="match_parent"/>
</LinearLayout>
这里的想法是将整个项目放在一个LinearLayout
中,按照内容的大小进行包装。依靠它将所有东西水平放置到位。然后仅将文本视图
包装在相对视图
中。只需让文本视图在彼此下方和侧面对齐即可。让RelativeLayout
担心它们实际上会相应地居中。它的重量,以确保它的延伸,以填补任何剩余的空间水平
此外,地图按钮可以是ImageButton。您不需要仅使用视图来获取项目单击事件。根布局本身可以按原样执行。您可能遇到了此设置的问题。查看它,它告诉您如何在嵌入ImageButton时正确获取要单击的行。谢谢您的帮助。我设法清理了很多布局文件
发生UI故障的原因是加载了错误的样式文件。只有在我加载另一个元素之前,它才使用那种样式。为了解决这个问题,我专门为listview创建了一个新样式,并将其分配给listview元素。使用XML中的wrap_内容您能为每个视图对XML进行注释吗?很难在你的图片中看到每一张图片都代表了什么。例如,您有3个TextView,但在picAlso中只能看到2个,您应该使用MATCH_PARENT,除非您需要支持API<8I不同意。我认为布局文件是问题所在。@JaySoyer:我为xml添加了一些注释。我还认为问题在布局文件中。几乎就好像当错误发生时,每个人的利润都会增加。