GridView numColumns = auto_fit,防止项目被拉伸

时间:2016-03-03 17:23:11

标签: android android-gridview

我想在对话框中显示项目。我用了GridView。如果我没有手动设置列宽,GridView使用硬编码的第2列,如图1所示。如果我设置了列宽,那么它会显示尽可能多的列,如图2所示。

问题是当我为项目设置硬编码宽度时,GridView似乎拉伸项目以填充整个屏幕(请参阅下面的代码)。我有什么可以阻止这个?我的最终目的是删除图2中的红色区域。这应该将对话宽度缩小红色区域的数量。

图1。 Without setting column width manually 图2。 After setting column width

my_dialogue.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content">
    <TableRow>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="Family"/>
        <GridView
            android:stretchMode="columnWidth"
            android:horizontalSpacing="0dp"
            android:numColumns="auto_fit"
            android:id="@+id/familyGrid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </TableRow>
</TableLayout>

my_item.xml

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

              android:background="@android:color/holo_red_dark"
              android:layout_height="wrap_content">
    <ImageView
        android:background="@android:color/holo_blue_dark"
        android:layout_width="48sp"
        android:layout_height="48sp"/>
    <TextView
        tools:text="Some application"
        android:id="@+id/tvLabel"
        android:background="@android:color/holo_blue_bright"
        android:gravity="center_vertical"
        android:layout_width="192sp"
        android:layout_height="48sp"/>
</LinearLayout>

MyDialogue.java

package com.example.me.test2;

import android.content.res.Resources;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import static android.util.TypedValue.COMPLEX_UNIT_SP;
import static android.util.TypedValue.applyDimension;

public class MyDialogue extends AppCompatDialogFragment
{
    String TAG = this.getClass().getName();

    private static MyDialogue instance;
    private GridView myGrid;

    public static MyDialogue getInstance()
    {
        if(instance == null)
        {
            instance = new MyDialogue();
        }
        return instance;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState)
    {
        setStyle(STYLE_NO_TITLE, 0);
        super.onCreate(savedInstanceState);
    }
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View v =  inflater.inflate(R.layout.my_dialogue, container, false);
        myGrid = (GridView)v.findViewById(R.id.familyGrid);

        //If I do not set the column width, it does not know the item width,
        //and the column number falls back to 2.
        Resources r = getResources();
        float px = applyDimension(COMPLEX_UNIT_SP, 192+48, r.getDisplayMetrics());
        myGrid.setColumnWidth((int)px);

        ArrayList<String> list = new ArrayList<>();
        list.add("Father: Homer Simpson");
        list.add("Mother: Marge Simpson");
        list.add("Son: Bart Simpson");
        list.add("Daughter: Lisa Simpson");
        list.add("Baby: Maggie Simpson");
        list.add("Dog: Santa's L. Helper");

        MyAdapter oa = new MyAdapter(list);

        myGrid.setAdapter(oa);

        return v;
    }

    class MyAdapter implements ListAdapter
    {
        List<String> list;
        public MyAdapter(List<String> list)
        {
            this.list = list;
        }
        @Override
        public boolean areAllItemsEnabled()
        {
            return true;
        }

        @Override
        public boolean isEnabled(int position)
        {
            return true;
        }

        @Override
        public void registerDataSetObserver(DataSetObserver observer)
        {

        }

        @Override
        public void unregisterDataSetObserver(DataSetObserver observer)
        {

        }

        @Override
        public int getCount()
        {
            return list.size();
        }

        @Override
        public Object getItem(int position)
        {
            return list.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return 0;
        }

        @Override
        public boolean hasStableIds()
        {
            return false;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            View v = View.inflate(myGrid.getContext(), R.layout.my_item, null);
            TextView tvLabel = (TextView) v.findViewById(R.id.tvLabel);
            tvLabel.setText(list.get(position));
            return v;
        }

        @Override
        public int getItemViewType(int position)
        {
            return 0;
        }

        @Override
        public int getViewTypeCount()
        {
            return 1;
        }

        @Override
        public boolean isEmpty()
        {
            return list.isEmpty();
        }
    }
}

MainActivity.java

package com.example.me.test2;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity
{
    String TAG = this.getClass().getName();
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnShow = (Button)findViewById(R.id.btnShow);
        btnShow.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                FragmentManager manager = getSupportFragmentManager();
                AppCompatDialogFragment dialogue = MyDialogue.getInstance();

                dialogue.show(manager, "dialogue");
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

如果我理解了你的问题,你可能想把gridView放在线性布局中,设置为网格视图来填充线性布局,但是给线性布局添加一些填充。祝你好运。