Android layout Android网格菜单布局

Android layout Android网格菜单布局,android-layout,Android Layout,我是android编程的初学者。(我正在使用android studio进行编码) 我正试图为我的android应用程序设计一个仪表板,从 它工作得很好,但我想按照 我需要2列布局与图标图像,标题1和标题2与背景图像。 有人能帮我吗 谢谢您必须使用BaseAdapter使用customGridView。在customGridView中,为每个列表项显示带有文本视图的ImageView main.xml <RelativeLayout xmlns:android="http://schema

我是android编程的初学者。(我正在使用android studio进行编码) 我正试图为我的android应用程序设计一个仪表板,从 它工作得很好,但我想按照 我需要2列布局与图标图像,标题1和标题2与背景图像。 有人能帮我吗


谢谢

您必须使用BaseAdapter使用customGridView。在customGridView中,为每个列表项显示带有文本视图的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>

输出:

最后,我找到了解决方案

您的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>
最后可能是这样的



你需要展示你迄今为止所做的尝试。我已经解决了这个问题。看看答案,看看它是怎样的:)谢谢,但它只是一个网格视图。不像这个参考:最好添加一个简短的解释,说明为什么或者如何解决问题。
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();

                       }
                      });


               }

}
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;

     }

}
<?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>
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);

    }
}
<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>