ana*_*und 22 android android-widget android-layout android-listview
这个很奇怪.我有一个列表视图,它是相对布局的一部分.我已为此相对布局设置了背景,并使列表视图背景变为透明.
现在,一切都很好,直到今天早上.即使我的列表视图中只有一行,我也可以看到整个屏幕都覆盖了我的自定义背景.
然后,我获得了Verizon Motorola Droid X for 2.3.3的更新(之前为2.2).一旦更新,我再次启动我的应用程序,现在发生了什么.
如果我的列表视图只有一行,我看到它下面有一个白色区域而不是我的自定义背景.但如果它说100行并因此覆盖整个屏幕我将不会看到奇怪的白色背景.我的相对布局的宽度和高度设置为"fill_parent".
我在底部发布了我的XML.有没有其他人遇到过这个问题,或者我犯了一些非常愚蠢的错误.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:background = "@drawable/background"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id = "@+id/listQueue"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:layout_below = "@id/homeScreenBanner"
android:background="@android:color/transparent"
android:divider="@drawable/separator"
android:scrollingCache="false"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
编辑:
我想我找到了解决这个问题的方法:
将layout_height属性更改为wrap_content,它就像一个魅力.:)
更改后的行.
android:layout_height = "wrap_content"
使用.xml布局文件中完全实现的基本布局,实际上可以轻松解决此问题.
首先,摩托罗拉的博客建议是不可接受的,因为它需要构建API v2.3才能使用' android:overScrollFooter'强制多个构建或大多数Android设备无法访问.
设置ListView的建议android:layout_height="wrap_content"在许多情况下都有效,如果ListView下面没有布局项,则非常有用.
当上面有一个或多个View以及ListView下面的一个或多个View时,会出现问题.这需要设置ListView android:layout_below和android:layout_aboveListView,它将ListView的高度拉伸到可用空间的高度,该空间大于必要的' wrap_content'.当发生这种情况时,"Bug"会启动 - ListView底部的额外空间将填充页脚的默认背景颜色,该颜色为深灰色且不透明.
现在为简单的解决方案.
将ListView放在垂直LinearLayout或等效的内部.允许LinearLayout从可用空间的顶部到底部跨越height="fill_parent",额外的将填充正确的背景可绘制或颜色.设置ListView的高度,"wrap_content"以便没有未使用的空间用于难看的灰色页脚背景.
这是一个例子:
<Button> android:id=@+id/MyTopButton" ... </Button>
<Button> android:id=@+id/MyBottomButton" android:layout_alignParentBottom="true" ... </Button>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_below="@+id/MyTopButton"
android:layout_above="@+id/MyBottomButton"
>
<!-- Note: This is a bug fix for a particular problem experienced on
Motorola OS v2.3.3 devices only in which the space below the unused
portion of the ListView is rendered in gray instead of the desired
transparent 'cacheColorHint' color. This solution which wraps the
ListView in another container (a LinearLayout in this case) allows
the ListView to be of height "wrap_content" while the parent
container is forced to be the desired height - namely from the
header area above down to the buttons below. -->
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/MyList"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我写了一个可以在Android 2.1/2中使用的类,它将使用ListViews的反射在2.3中做正确的事情:
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.ListView;
public class TransparentListView extends ListView {
private void makeTransparent() {
if (Build.VERSION.SDK_INT >= 9) {
try {
Method overscrollFooterMethod =
TransparentListView.class.getMethod("setOverscrollFooter", new Class[] {Drawable.class});
Method overscrollHeaderMethod =
TransparentListView.class.getMethod("setOverscrollHeader", new Class[] {Drawable.class});
try {
overscrollFooterMethod.invoke(this, new Object[] {null});
overscrollHeaderMethod.invoke(this, new Object[] {null});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} catch (SecurityException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
public TransparentListView(Context context) {
super(context);
this.makeTransparent();
}
public TransparentListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.makeTransparent();
}
public TransparentListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.makeTransparent();
}
}
Run Code Online (Sandbox Code Playgroud)
在XML中使用它如下:
<com.myapp.TransparentListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:transcriptMode="alwaysScroll"
android:layout_weight="1"
android:dividerHeight="0dip"
android:divider="#00000000"
android:cacheColorHint="#00000000"
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6182 次 |
| 最近记录: |