我想显示标签,我正在使用gridview,但采取相同的列宽,但我不想要这个

时间:2014-01-17 05:23:24

标签: android android-gridview

我想显示标签,我在这个列宽度中使用网格视图相同,但我不想根据我要显示的文本保持列宽相同。我可能会尝试这个。 我想显示如下图所示的文字如何实现

public class MainActivity extends Activity {
    Imagehelper controller;
    EditText mAutoSearch;

    ArrayList<String> selContactList = new ArrayList<String>();
    SimpleStringAdapter adapter;
    GridView gridView;

    private Button save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAutoSearch = (EditText) findViewById(R.id.et_autoComplete);
        save = (Button) findViewById(R.id.save);
        gridView = (GridView) findViewById(R.id.grid);
        adapter = new SimpleStringAdapter(this, 0, selContactList);
        gridView.setAdapter(adapter);
        save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String item = mAutoSearch.getText().toString();
                selContactList.add(item);

                adapter.setNewList(selContactList);
                mAutoSearch.setText("");
            }
        });
    }

    public class SimpleStringAdapter extends BaseAdapter {

        Context activity;
        // public ArrayList<String> list;
        Typeface normaltypeface;
        LayoutInflater layoutinflator;

        public SimpleStringAdapter(Context context, int textViewResourceId,
                ArrayList<String> selContactList) {
            // TODO Auto-generated constructor stub
            activity = context;
            // list = locations;

            layoutinflator = (LayoutInflater) activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public void setNewList(ArrayList<String> selContactList) {
            // list = objects;
            this.notifyDataSetChanged();
        }

        @Override
        public View getView(final int position, View convertView,
                ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                holder = new ViewHolder();

                convertView = layoutinflator.inflate(R.layout.gridview, null);
                holder.desc_tv = (TextView) convertView
                        .findViewById(R.id.genderdesc);
                holder.imgbtn = (ImageButton) convertView
                        .findViewById(R.id.imageButton1);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            String gender = getItem(position);

            holder.desc_tv.setText(gender);

            holder.imgbtn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    String s = getItem(position);

                    System.out.println("String item position: " + s);

                    notifyDataSetChanged();
               }
            });

            return convertView;
        }

        class ViewHolder {
            TextView desc_tv;
            ImageButton imgbtn;
        }

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

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

        @Override
        public String getItem(int position) {
            return selContactList.get(position);
        }
    }
}

我的主要xml是:

<LinearLayout 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal"
        android:weightSum="1" >

        <EditText
            android:id="@+id/et_autoComplete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:hint="Search"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" />

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Save" />
    </LinearLayout>

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:columnWidth="90dip"
        android:gravity="center"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" >
    </GridView>

</LinearLayout>

我的gridview xml是:

<LinearLayout 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:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:orientation="horizontal"
        android:weightSum="1" >

        <EditText
            android:id="@+id/et_autoComplete"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:hint="Search"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" />

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:text="Save" />
    </LinearLayout>

    <GridView
        android:id="@+id/grid"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:columnWidth="90dip"
        android:gravity="center"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth"
        android:verticalSpacing="5dp" >
    </GridView>

</LinearLayout>

我希望布局看起来像下面的一样。 enter image description here

我怎样才能实现它?

0 个答案:

没有答案