【问题标题】:ImageView and Listview - AndroidImageView 和 Listview - Android
【发布时间】:2012-07-04 08:49:43
【问题描述】:

美好的一天!!!

我在 android 的横向布局中遇到了问题。

当我要将方向更改为横向时,ImageView 似乎处于固定位置,我只能在 listview 中看到滚动条...我只想有一个布局,imageview 也可以滚动以便我可以清楚地看到列表项。

这是我的布局 xml

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="fill_parent"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
    android:layout_height="wrap_content"
    android:contentDescription="@string/app_name"
    android:src="@drawable/banner" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/imageView1"
        android:drawSelectorOnTop="true" />

</RelativeLayout>  

【问题讨论】:

    标签: android android-layout android-listview android-imageview


    【解决方案1】:

    这有点棘手,因为您不能将列表视图放在滚动视图中,但是如果您创建一个可以具有不同类型行的自定义适配器(例如,仅包含文本的行,具有ImageView 等)。

    然后让 ListView 中的第一行始终是图像。

    查看此tutorial(部分:不同列表项的布局)以了解如何制作不同类型的行。

    【讨论】:

    • 你能给我一个适合我需要的示例 xml 布局
    • 这不能在布局中完成。您必须使用自定义适配器将图像放在列表视图的第一行。
    【解决方案2】:

    我认为在 ScrollView 中使用 ListView 不是一件好事如果您希望该图像也应该滚动而不是这样:

    1) 对于 Listview,您需要一个自定义 adapter,其中在自定义适配器类的 getview() 方法中,您必须这样做

     public View getView(int position, View convertView, ViewGroup parent)
        {
    
        LayoutInflater inflater = getLayoutInflater();
        View row;
    
        row = inflater.inflate(R.layout.listview_rowlayout, parent, false);
    
        TextView textview = (TextView) row.findViewById(R.id.tv_list);
        ImageView imageview = (ImageView) row.findViewById(R.id.iv_list);
    
        textview.setText(data_text[position]);
        if(position==0){
        imageview.setImageResource(R.drawable.YourimageName);
        }else{
         imageview.setVisibility(View.GONE);
        }
      return (row);
    
    }  
    

    你的 listview_rowlayout.xml 是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical" >
    
        <ImageView
            android:id="@+id/iv_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParenttop="true"
            android:layout_marginLeft="10dp"
           />
    
        <TextView
            android:id="@+id/tv_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:textColor="#000000"
            android:layout_alignBottom="@+id/imageView1"
            android:text="" />
    
    
    </RelativeLayout>
    

    您在其中添加了列表视图的 ma​​in.xml 应该是这样的

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="true" />
    
    </RelativeLayout>  
    

    【讨论】:

    • 感谢您的评论......并在 benito 提供的链接的帮助下对如何解决它有了一些想法。我为行项目创建了两个 xml 布局,其中一个布局用于我的 imageview 和我的列表项。我禁用了第一个项目,以便无法单击它。无论如何,谢谢你的想法......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 2019-07-26
    • 2017-08-02
    • 2013-04-19
    相关资源
    最近更新 更多