Android图片视图和列表视图

时间:2013-11-11 19:57:05

标签: android imageview

我想做类似文件管理器的事情。我有一个文件列表,当我点击一个文件,如果它是一个图像,我必须在预览中显示它。所以,我做了一个表格布局,其中我有两行:一个是包含所有文件的列表视图,第二个是我想要显示图像的图像视图。问题是当我将图像添加到图像视图时它根本不显示。这是布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </ImageView>
    </TableRow>
</TableLayout>

这里是我将图像添加到ImageView的地方(我必须这样做,因为我的文件在SD卡上)

ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(file));

有人能告诉我我做错了什么吗?感谢

1 个答案:

答案 0 :(得分:3)

您最好使用LinearLayout。这样会更有效率。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

但是如果你必须使用TableLayout(我看不出你的样本的任何理由),你可以像这样使用:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ListView
            android:id="@+id/listView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </TableRow>

    <TableRow
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="bottom" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" >
        </ImageView>
    </TableRow>

</TableLayout>