【问题标题】:Why isn't my ListView/Dialog transparent?为什么我的 ListView/Dialog 不透明?
【发布时间】:2012-06-05 17:34:33
【问题描述】:

EDIT~ 找到解决方案,并在我下面的回复中(现在是最后一个回复),但直到两天后我才能将其标记为问题的答案。同时,我会在此处保留此编辑,直到我可以将其标记为正确为止。

对话框的调用位置:

public OnClickListener fieldListener = new OnClickListener() {
    public void onClick(View v) {
        FragmentManager fm = getSupportFragmentManager();
        switch (v.getId()) {
        case (0) :

        ChooseDialog ageDialog = new ChooseDialog("Age", ageEditText, act);
        ageDialog.show(fm, "fragment_edit_name");

对话框:

public class ChooseDialog extends DialogFragment implements DialogInterface.OnClickListener, OnMultiChoiceClickListener {

public Dialog onCreateDialog(Bundle savedInstanceState) {
        LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View dialogLayout = inflater.inflate(R.layout.dialog, null);

        AlertDialog.Builder builder;
        switch (editText.getId()) {
        case (0) :
            ListView list = (ListView) dialogLayout.findViewById(R.id.listView1);
        list.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist, 
                activity.getResources().getStringArray(R.array.ageArray)));
        list.setBackgroundColor(00000000);
        builder = new AlertDialog.Builder(activity);
        builder.setTitle(type);
        builder.setNegativeButton("Cancel", this);
        builder.setView(dialogLayout);
        /* builder.setAdapter(new ArrayAdapter<String>(activity, R.layout.dialoglist, 
                    activity.getResources().getStringArray(R.array.ageArray)), 
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    editText.setText(activity.getResources().getStringArray(R.array.ageArray)[which]);
                }} 
                );*/
        return builder.create();

Dialog.xml:

<?xml version="1.0" encoding="UTF-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listView1"
    android:background="#00000000"
    android:cacheColorHint="#00000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

DialogList.xml(只是适配器的文本视图):

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:paddingLeft="6dip"
    android:textColor="@color/androidBlue"
    android:paddingRight="10dip"
    android:layout_weight="1"
    android:textAppearance="?android:attr/textAppearanceLarge" />

【问题讨论】:

  • 您是否尝试过通过代码或样式来处理Dialog 的窗口?您可以更改几个与背景相关的选项。 this answer中提到了几个。

标签: android android-listview android-fragments android-alertdialog


【解决方案1】:

试试

list.setBackgroundColor(Color.TRANSPARENT);

编辑:

在浏览几期 ICS 时,我遇到了this,我认为这可能是 ICS 的问题。认为这个问题与问题无关,但它表明开发人员在设置背景时遇到了问题。

随时欢迎指正。

【讨论】:

  • 没有骰子,结果与 0 相同,对话框和列表的背景仍然是 ICS 灰色。
  • 在你的文本视图中尝试 android:background="@null"
  • dialoglist textview 的背景仍然没有变化 = @null :/
  • 你能把listview的对话框截图发一次吗!我似乎没有发现代码有任何问题!
  • 将屏幕截图编辑到原始帖子中,它在 ICS GNexus 上运行 - 在 ICS emu 上看起来完全一样,现在在 Ginger 上进行测试。 (图片中包含您要求的所有代码)
【解决方案2】:

我的解决方案:将 AlertDialog.Builder 构造函数更改为使用带有 Context, Theme 的构造函数:

builder = (Integer.parseInt(android.os.Build.VERSION.SDK) < 11)? new AlertDialog.Builder(activity) : 
        new AlertDialog.Builder(activity, android.R.style.Theme_Translucent);

这确保它在 Ginger 上加载透明(通过 XML 中的透明行);和半透明的 ICS。主要问题是对话框正面和负面按钮与我的 UI 不匹配。未来几天我将在那里开发解决方案。

另一个问题是使用builder.setMultiChoiceItems(), null, this); 而不是列表。这使它在整个对话区域具有全白色背景。我也希望在这里发现一个主题方法,但除此之外,我所有的对话框看起来都是我现在想要的方式:P

在此处找到解决方案:https://stackoverflow.com/a/8687164/1231943

【讨论】:

    【解决方案3】:

    对于 XML:

    android:background="@android:color/transparent"
    android:cacheColorHint="@android:color/transparent"
    

    【讨论】:

    • 在 Ginger 上仍然是透明的,在 ICS 上是灰色的。我相信上面的 Sana 可能已经发现了 ICS 错误的问题。
    • 是的,可能是。但请尝试删除 list.setBackgroundColor();来自java的行并设置列表和文本视图都是bg颜色透明......
    • 仍然没有运气,列表和文本视图是透明的,Java 根本没有更改背景。 Ginger 看起来仍然很好,但 ICS 仍然是灰色的。
    • 太好了,谢谢,接下来几个小时我会自己寻找答案。考虑为 Sana 链接的问题加注星标!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-29
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多