GridView在portratit中每行有1个ImageView,在横向模式下有2个ImageView

时间:2012-09-17 13:53:31

标签: android android-layout imageview landscape-portrait

我实际上有一个由自定义ArrayAdapter提供的ListActivity。它构建由单个ImageViews创建的行。在纵向模式下它看起来不错。但在横向模式下,它会将图像拉伸到边框(我将scaleType设置为fitXY) 我希望在横向模式下每行有2个ImageView。 GridView是正确的布局吗? 你会怎么做?

由于我有一个480x800的屏幕,纵向,ImageView的宽度为480px,而在横向上,两者的宽度均为400px。这不是问题,但重要的是要尊重宽高比。

5 个答案:

答案 0 :(得分:3)

为什么不使用Gridview来完成任务。如果要使用列表视图,则必须根据方向检查方向

1)在横向模式下,将加载layout-land /的布局,其中包含2个图像视图。

2)在纵向模式下,将加载布局端口/的布局,其中包含1个图像视图。

答案 1 :(得分:2)

是的,GridView是最好最简单的方法。您现在可以对ArrayAdapter使用相同的自定义OnClickListenerListView,只需要更改

ListView myList = (ListView) findViewById(R.id.list);

GridView myGrid = (GridView) findViewById(R.id.gridview);

和xml类似:

 <GridView
        android:id="@+id/gridview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnWidth="390px"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />

现在,您应该避免使用“px”作为单位,而是使用“dp”代替,并且只有在您确定自己的应用程序仅针对480x800分辨率设计时才会使用。

答案 2 :(得分:1)

gridView不是一个布局。它是一个adapterView,因此它的目的是能够“显示”大量的视图,并且能够在它们之间滚动。

也许您想使用gridLayout代替?它还有一个可比性库......

无论如何,如果你想坚持使用gridView,只需将android:numColumns设置为通过纵向模式的values文件夹设置的变量,并将value-land设置为landscape所需的值< / p>


编辑:这是使用gridView的解决方案:

布局xml文件应包含以下内容:

<GridView android:layout_width="match_parent"
  android:numColumns="@integer/cols" android:layout_height="match_parent"
  />

res/values文件夹中创建一个xml文件(例如“consts.xml”),其中包含以下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <item type="integer" name="cols">2</item>
</resources>

另外,在res/values-land文件夹中创建相同的文件,并将2设置为3。

答案 3 :(得分:1)

您可以为不同的方向提供不同的资源。因此,当设备处于横向状态时,您会在ListView中填充每个项目中具有两个ImageView s的布局,并且纵向布局中每个项目的布局为ImageView

查看Providing Resources,了解如何为不同的方向提供不同的布局XML。

示例代码:

// Create an anonymous implementation of OnClickListener
private OnClickListener mImageListener = new OnClickListener() {
    public void onClick(View v) {
      // do something when the ImageView is clicked
    }
};

//Check the orientation and handle accordingly in the getView
public View getView (int position, View convertView, ViewGroup parent){
    if(convertView == null){
        convertView = (LinearLayout)inflater.inflate(R.layout.list_item, parent);
    }
    ImageView leftImage = (ImageView)convertView.findViewById(R.id.leftImage);
    ImageView rightImage = (ImageView)convertView.findViewById(R.id.rightImage);
    boolean isTwoColumn = (rightImage != null);

    //If it is two column layout, set both ImageViews
    if(isTwoColumn){
      leftImage.setImageResource(...);
      leftImage.setOnClickListener(mImageListener);
      rightImage.setImageResource(...);
      rightImage.setOnClickListener(mImageListener);
    }else{
      leftImage.setImageResource(...);
      leftImage.setOnClickListener(mImageListener);
    }
}

/res/layout-land/list-item.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
      android:id="+@id/leftImage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
    <ImageView
      android:id="+@id/rightImage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
</LinearLayout>

/res/layout-port/list-item.xml

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <ImageView
      android:id="+@id/leftImage"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content">
</LinearLayout>

答案 4 :(得分:0)

我尝试了Gridview并亲自发现它非常繁琐地获得了我所追求的显示器(内部较小的单元格的比率正确显示)...经过数周的不同分辨率和屏幕格式的重新调整我选择了创建我自己的网格视图从头开始,没有回头。

我不是说Gridview不是没有它的优点但我能说的是实现你自己的系统需要花费几个小时,如果你有一个非常明确的想法在你的脑海里它应该如何运作你可能会发现它更容易编码自己。

本质上它只是一个派生自ViewGroup的类,覆盖onMeasure和onLayout项目来测量它包含的子视图,然后将它们放到屏幕上你想要的位置。

如果您正在寻找长列表,但我仍然会查看列表视图,或者看看gridview可能值得一试,但如果您需要,可以放心控制所有这些,这当然是一种安心。

相关问题