GridView,图片和文字位于中心

时间:2017-01-12 17:28:46

标签: android android-layout gridview android-imageview

您好我想开发这种类型的布局,但我很困惑,就是gridview或imageviews这样对齐。 好吧,我的问题是如何在我正在工作的片段中实现这种类型的菜单

enter image description here

https://www.learn2crack.com/2014/01/android-custom-gridview.html

实施网格视图后

我得到了这个像屏幕

enter image description here

如何让gridView看起来像第一张图片?

作为块中心的文本我真的想实现它。

grid_single.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="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/grid_image"
        android:layout_width="match_parent"
        android:src="@drawable/denist_3"
        android:layout_height="50dp">
    </ImageView>

    <TextView
        android:id="@+id/grid_text"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:layout_gravity="center"
        android:text="Dentist"
        android:textSize="9sp" >
    </TextView>

</LinearLayout>

main_act.xml:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
       <GridView
        android:numColumns="auto_fit"
        android:layout_below="@+id/comsp"
        android:gravity="center"
        android:columnWidth="150dp"
        android:stretchMode="columnWidth"
        android:layout_width="fill_parent"
        android:layout_height="600dp"
        android:id="@+id/grid"
        />

</RelativeLayout>

Adapterclass.java

public class CustomGrid extends BaseAdapter{
    private Context mContext;
    private final String[] web;
    private final int[] Imageid;

    public CustomGrid(Context c,String[] web,int[] Imageid ) {
        mContext = c;
        this.Imageid = Imageid;
        this.web = web;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return web.length;
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View grid;
        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            grid = new View(mContext);
            grid = inflater.inflate(R.layout.grid_single, null);
            TextView textView = (TextView) grid.findViewById(R.id.grid_text);
            ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
            textView.setText(web[position]);
            imageView.setImageResource(Imageid[position]);
        } else {
            grid = (View) convertView;
        }

        return grid;
    }
}

Mainactivity.java

     CustomGrid adapter = new CustomGrid(getActivity(), web, imageId);

 grid=(GridView)v.findViewById(R.id.grid);
 grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                Toast.makeText(getActivity(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();

            }
        });

2 个答案:

答案 0 :(得分:1)

您的xml文件太复杂了。您只需在活动/片段布局中使用GridView即可实现此目的。然后,您需要为Gridview提供一个自定义适配器类来为另一个xml文件充气,该文件只包含一个imageview和textview。 您可以点击此链接开始使用GridView:https://www.learn2crack.com/2014/01/android-custom-gridview.html

答案 1 :(得分:1)

要获得叠加布局,您可以使用FrameLayout,因为它的子视图会叠加在一起。在您的情况下,item_layout.xml看起来像这样

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

    <ImageView
        android:id="@+id/ivBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/myimage"/>

    <TextView
        android:id="@+id/tvOverlayText"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/mytext"
        android:gravity="center"/>

</FrameLayout>