我想知道如何使用菜单中的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
机器人。 下拉菜单IconsAndroid自定义垂直下拉列表。在android示例中带有图标的弹出菜单。 Android自定义垂直下拉菜单图标。 Android自定义垂直下拉菜单图标&#34; /&GT;
<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>
答案 0 :(得分:3)
您需要使用微调器,这将导致使用适配器和几个布局。 首先在您的activity_main.xml中:
import { SplashScreen } from 'directory/splash-screen';
然后在/ res / layout文件夹下创建一个新布局,只需将其命名为custom_spinner_layout.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>
然后在你的MainActivity.java中你需要做你的代码:
<?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>
您需要创建一个新的java类文件,并将其命名为CustomAdapter.java,这将从BaseAdapter扩展:
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
}
}
您可以获取以下工作代码: https://github.com/edgebasis/spinnerExample