Android:listview有一个奇怪的宽度

时间:2014-12-12 13:49:02

标签: android android-layout android-listview android-adapter

在我的应用中,我在屏幕中央的RelativeLayout中创建了一个Listview。这个RelativeLayout位于屏幕末端的中心,里面有一个ImageView广告背景和ListView。

这是我想要获得的结果:

enter image description here

这是我获得的结果:

enter image description here

红色是ListView的背景颜色,因此您可以看到它的大小。

我的活动xml是

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#70000000"
    tools:context="com.example.example.RecordActivity">



    <RelativeLayout
        android:id="@+id/content_record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/back_record"
            android:layout_centerInParent="true"/>

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:dividerHeight="0dp"
            android:divider="@null"
            android:background="#70000000">

        </ListView>


    </RelativeLayout>


</RelativeLayout>

我的适配器布局是

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textViewList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="row"
        android:padding="20dip"
        android:layout_centerInParent="true"
        android:textSize="22dip"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/line"
        android:layout_centerInParent="true"/>

</LinearLayout>

出了什么问题?如何在第一张图片中获得结果?

由于

3 个答案:

答案 0 :(得分:1)

将您的活动xml更改为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#70000000"
    tools:context="com.example.example.RecordActivity">


<FrameLayout
    android:id="@+id/content_record"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/back_record"
        android:layout_centerInParent="true"/>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:dividerHeight="0dp"
        android:divider="@null"
        android:background="#70000000">

    </ListView>


</FrameLayout>

或删除imageView并将listView的背景设置为

android:background="@drawable/back_record"

答案 1 :(得分:0)

content_record RelativeLayout的高度和宽度更改为match_parent以全屏显示。

编辑:: 为listview提供特定宽度

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#70000000"
        tools:context="com.example.example.RecordActivity">



        <RelativeLayout
            android:id="@+id/content_record"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:background:"@drawable/back_record">



            <ListView
                android:id="@+id/listView"
                android:layout_width="750dp"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:dividerHeight="0dp"
                android:divider="@null"
                android:background="#70000000">

            </ListView>


        </RelativeLayout>


    </RelativeLayout>

答案 2 :(得分:0)

这样做可以清理一些

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_record"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#70000000"
    tools:context="com.example.example.RecordActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:dividerHeight="3dp"
        android:divider="@android:color/black"
        android:background="@drawable/back_record" />

</RelativeLayout>

#700000000的背景似乎有点奇怪,有原因吗?它是应用程序中所有内容的背景吗?如果是,you might consider setting your app theme's background to @color/main_background and adding a color xml of <color name="main_background">#70000000</color>