【问题标题】:List item width does not match parent inside dialog列表项宽度与对话框内的父项不匹配
【发布时间】:2015-07-24 11:57:09
【问题描述】:

我正在尝试仅在对话框中使用字符串对象制作一个简单的 listView;问题是列表项不是我设置的宽度match_parent

列表视图是根元素:

<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#ff0000"
    android:id="@+id/lvLinks"
    android:layout_height="match_parent"/ >

列表项:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:padding="7dp"
    android:textSize="15sp"
    android:background="#0000ff"
    android:layout_height="match_parent" />

适配器:

ListView lv = (ListView) findViewById(R.id.lvLinks);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,R.layout.list_item_links,items);
lv.setAdapter(adapter);

我已经在很多操作系统上测试过,这不是问题

已修复answer

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:background="#ff0000"
        android:id="@+id/lvLinks"
        android:layout_height="match_parent" />
</LinearLayout>

【问题讨论】:

  • 请提供有关如何创建对话框的代码。是DialogFragment吗?
  • 我认为 textview 的主要布局是 wrap_content 所以它没有占用全宽所以检查一下。
  • 我对 DialogFragment 有同样的问题,建议的答案不起作用

标签: android android-listview


【解决方案1】:

为什么不将 ListView 放在具有宽度 match_parent 或 fill_parent dialog.xml 的 LinearLayout 或 RelativeLayout 中

【讨论】:

  • 是的@itzhar,但你也需要一些主布局,你可以检查它可能对你有帮助。设置它的方向 = 垂直,并将宽度设置为“match_parent”。
  • 这也不起作用,有时项目仍然没有被拉伸。经典的谷歌,这么多的错误......
  • @qkx 添加您的代码。然后我们也可以解决它。
  • 在活动中这不是问题,但在带有自定义布局的对话框中它不起作用 bcos android 源代码中存在错误。你可以试试看 :) 它不能在 XML 中修复(只能通过代码中的变通方法),stackoverflow 中有很多类似的主题。对不起,我应该在第一个答案中更准确地说,在 DIALOGS 中它不起作用
【解决方案2】:

这可能不是确切的解决方案,但您可以尝试下面的 xml 列表项

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
<TextView 
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:padding="7dp"
    android:textSize="15sp"
    android:background="#0000ff"
    android:layout_height="wrap_content" /> 
</LinearLayout>

【讨论】:

  • 无需创建自定义适配器,因为它具有 android textview 默认 id android:id="@android:id/text1"。您可以将此布局与 ArrayAdapter 一起使用。
  • 对话框甚至无法使用该布局打开
【解决方案3】:

建议的答案没有为我解决任何问题。这个问题实际上只出现在 MIUI 设备上,而不是谷歌的股票、三星或一加。

相反,我将以下代码添加到我的自定义 listView 构造函数中:

    addOnLayoutChangeListener(this);

还有听者:

@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)
{
    int I = getChildCount();
    for(int i = 0; i < I; i++)
    {
        getChildAt(i).requestLayout();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2012-10-28
    相关资源
    最近更新 更多