【问题标题】:Android Grid Menu LayoutAndroid 网格菜单布局
【发布时间】:2023-03-14 21:16:01
【问题描述】:

我是 android 编程的初学者。(我正在使用 android studio 进行编码) 我正在尝试为我的 android 应用程序设计一个仪表板,并接受 following link 的指导 它运作良好,但我想按照 Like This Image 我需要带有图标图像的 2 列布局,标题 1 和带有背景图像的标题 2。 谁能帮我。

谢谢你

【问题讨论】:

  • 你需要展示你到目前为止所做的尝试。
  • 我解决了这个问题。查看答案以了解它的效果:)

标签: android-layout


【解决方案1】:

您必须通过 BaseAdapter 使用 customGridView。在 customGridView 中,为每个列表项显示带有 TextView 的 ImageView。

main.xml

<RelativeLayout 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"
    tools:context=".MainActivity" >

   <GridView
        android:id="@+id/gridViewCustom"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:columnWidth="80dp"
        android:gravity="center"
        android:numColumns="auto_fit"
        android:stretchMode="columnWidth" />


</RelativeLayout>

grid_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="5dp" >

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginRight="10dp"
        android:src="@drawable/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:textSize="15sp" >
    </TextView>

</LinearLayout>

CustomGridViewMainActivity.java

public class CustomGridViewMainActivity extends Activity 
{


            GridView gridView;
            GridViewCustomAdapter grisViewCustomeAdapter;


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


                    gridView=(GridView)findViewById(R.id.gridViewCustom);
                    // Create the Custom Adapter Object
                    grisViewCustomeAdapter = new GridViewCustomAdapter(this);
                    // Set the Adapter to GridView
                    gridView.setAdapter(grisViewCustomeAdapter);

                    // Handling touch/click Event on GridView Item
                      gridView.setOnItemClickListener(new OnItemClickListener() {

                       @Override
                       public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
                           String selectedItem;
                           if(position%2==0)
                               selectedItem="Facebook";
                           else
                               selectedItem="Twitter";
                        Toast.makeText(getApplicationContext(),"Selected Item: "+selectedItem, Toast.LENGTH_SHORT).show();

                       }
                      });


               }

}

然后使用您的自定义视图设置适配器

GridViewCustomAdapter.java

public class GridViewCustomAdapter extends ArrayAdapter
{

Context context;

         public GridViewCustomAdapter(Context context) 
     {
             super(context, 0);
             this.context=context;

     }

     public int getCount() 
        {
                     return 24;
        }

     @Override
     public View getView(int position, View convertView, ViewGroup parent) 
     {
             View row = convertView;

             if (row == null) 
             {
                     LayoutInflater inflater = ((Activity) context).getLayoutInflater();
                     row = inflater.inflate(R.layout.grid_row, parent, false);


                     TextView textViewTitle = (TextView) row.findViewById(R.id.textView);
                     ImageView imageViewIte = (ImageView) row.findViewById(R.id.imageView);

                     if(position%2==0)
                     {
                             textViewTitle.setText("Facebook");
                             imageViewIte.setImageResource(R.drawable.facebook);
                     }
                     else
                     {
                             textViewTitle.setText("Twitter");
                             imageViewIte.setImageResource(R.drawable.twitter);
                     }
             } 



      return row;

     }

}

输出:

【讨论】:

【解决方案2】:

终于找到了解决办法……

您的 XML 文件`

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/bg"
    android:padding="16dp">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ededed"
            android:drawableLeft="@drawable/ic1"
            android:paddingBottom="5dp"
            android:paddingLeft="16dp"
            android:paddingRight="40dp"
            android:paddingTop="8dp"
            android:text="\nButton"
            android:layout_marginBottom="5dp"
            android:textColor="#000000" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="#ededed"
            android:drawableLeft="@drawable/ic1"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="40dp"
            android:paddingTop="8dp"
            android:text="\nButton"
            android:textColor="#000000" />




    </LinearLayout>


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#ededed"
            android:drawableLeft="@drawable/ic1"
            android:paddingBottom="5dp"
            android:paddingLeft="16dp"
            android:paddingRight="40dp"
            android:paddingTop="8dp"
            android:text="\nButton"
            android:layout_marginBottom="5dp"
            android:textColor="#000000" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginBottom="5dp"
            android:background="#ededed"
            android:drawableLeft="@drawable/ic1"
            android:paddingBottom="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="40dp"
            android:paddingTop="8dp"
            android:text="\nButton"
            android:textColor="#000000" />




    </LinearLayout>


</LinearLayout>

` 然后是 AndroidButtonWithIconAndText.java 类

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class AndroidButtonWithIconAndText extends AppCompatActivity {

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

    }
}

最后可能是这个样子

【解决方案3】:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#000" />

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#fff" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#fff" />

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#000" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#000" />

    <Button
        android:layout_width="0dp"
        android:layout_height="120dp"
        android:layout_weight="1"
        android:textColor="#fff" />

</LinearLayout>

【讨论】:

  • 最好添加一个简短的解释,说明为什么或如何解决问题。