gpt4 book ai didi

android - 如何使用 Gridview 创建带菜单的垂直下拉菜单?

转载 作者:搜寻专家 更新时间:2023-11-01 08:23:57 25 4
gpt4 key购买 nike

我想知道如何使用菜单中的 GridView 选项开发自定义下拉图标菜单。

这是我自定义向下图标的代码

<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#c2ec97"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">

<Button
android:id="@+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="@android:drawable/arrow_down_float"
android:onClick="verticalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dropdown_custom_icon_menu"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Vertical Dropdown Menu IconsAndroid Custom Vertical. Break line after Icon in Menu Item

Android. Dropdown Menu IconsAndroid Custom Vertical Dropdown. popup menu with icon in android example. Android Custom Vertical Dropdown Menu Icons. Android Custom Vertical Dropdown Menu Icons" />

        <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Vertical Dropdown Menu Icons" />
</LinearLayout>

<LinearLayout
android:id="@+id/vertical_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dropdown_custom_icon_menu"
android:background="#333"
android:orientation="vertical"
android:padding="3dp"
android:visibility="invisible">

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_send"
android:text="Android Vertical Custom DropDown Menu" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_refresh"
android:text="Android Vertical Custom DropDown Menu" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="@drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="@drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />

</LinearLayout>
</RelativeLayout>

这就是我所拥有的: enter image description here这就是我想要的:

enter image description here

最佳答案

您需要使用微调器,这将导致使用适配器和几个布局。首先在您的 activity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">

<Spinner
android:id="@+id/sSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />

</RelativeLayout>

然后在/res/layout 文件夹下创建一个新布局,并将其命名为 custom_spinner_layout.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="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/myImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="10dp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="test"
android:textColor="#000" />
</LinearLayout>

然后在您的 MainActivity.java 中,您需要执行您的代码:

    import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{


String[] carNames={"Chevy","Dodge","Mazda","Honda","BMW","Toyota"};
int icons[] = {R.drawable.Chevy, R.drawable.Dodge, R.drawable.Mazda, R.drawable.Honda, R.drawable.BMW, R.drawable.Toyota};

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


Spinner spin = (Spinner) findViewById(R.id.sSpinner);
spin.setOnItemSelectedListener(this);

CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),icons,carNames);
spin.setAdapter(customAdapter);
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
//your code goes here///
Toast.makeText(getApplicationContext(), carNames[position], Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// something happens here
}
}

您将需要创建一个新的 java 类文件并将其命名为 CustomAdapter.java,这将从 BaseAdapter 进行扩展:

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {
Context context;
int icons[];
String[] carNames;
LayoutInflater inflter;

public CustomAdapter(Context applicationContext, int[] icons, String[] carNames) {
this.context = applicationContext;
this.icons = icons;
this.carNames = carNames;
inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
return icons.length;
}

@Override
public Object getItem(int i) {
return null;
}

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(icons[i]);
names.setText(carNames[i]);
return view;
}
}

您可以在以下位置获取工作代码: https://github.com/edgebasis/spinnerExample

关于android - 如何使用 Gridview 创建带菜单的垂直下拉菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47250955/

25 4 0