如何创建下拉列表?我试过ScrollView,但它不是我所需要的。
当前回答
这个代码对我来说很好,希望它也能帮助到你。
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dip" >
<TextView
android:id="@+id/spinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp"
android:background="#FF00FF">
<Spinner
android:id="@+id/dropStatus"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_marginBottom="7dp"
android:drawSelectorOnTop="true"/>
</LinearLayout>
适配器类:
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StatusAdapter extends ArrayAdapter<StatusBean> {
private Context context;
private ArrayList<StatusBean> statuses;
public Resources res;
StatusBean currRowVal = null;
LayoutInflater inflater;
public StatusAdapter(Context context,
int textViewResourceId, ArrayList<StatusBean> statuses,
Resources resLocal) {
super(context, textViewResourceId, statuses);
this.context = context;
this.statuses = statuses;
this.res = resLocal;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.status_item, parent, false);
currRowVal = null;
currRowVal = (StatusBean) statuses.get(position);
TextView label = (TextView) row.findViewById(R.id.spinnerItem);
if (position == 0) {
label.setText("Please select status");
} else {
label.setText(currRowVal.getStatus());
}
return row;
}
}
StatusBean class:
public class StatusBean {
private String status;
private String statusCode;
public StatusBean() {
}
public StatusBean(String status,
String statusCode) {
this.status = status;
this.statusCode = statusCode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
}
活动班级:
在onCreate方法内部:
static ArrayList<StatusBean> STATUS_LIST = new ArrayList<StatusBean>();
for(int i=0;i<=10;i++) {
STATUS_LIST.add(new StatusBean(“Status ”+i, “Stattus ”+i));
}
final Spinner dropStatus = (Spinner)findViewById(R.id.dropStatus);
Resources res = getResources();
StatusAdapter adapter = new StatusAdapter(this, R.layout.item, SessionData. STATUS_LIST, res);
dropStatus.setAdapter(adapter);
其他回答
在Kotlin中,您可以这样做:
首先,将这些代码放入布局中
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
然后你可以在onCreate()在活动作为->
val spinner = findViewById<Spinner>(R.id.spinner)
val items = arrayOf("500g", "1kg", "2kg")
val adapter = ArrayAdapter<String>(
this,
android.R.layout.simple_spinner_dropdown_item,
items
)
spinner.setAdapter(adapter)
你可以从下拉菜单中获取listener:
spinner.onItemSelectedListener = object : OnItemSelectedListener {
override fun onItemSelected(
arg0: AdapterView<*>?,
arg1: View?,
arg2: Int,
arg3: Long
) {
// Do what you want
val items = spinner.selectedItem.toString()
}
override fun onNothingSelected(arg0: AdapterView<*>?) {}
}
要将你的列表动态添加到旋转器中,就像从webservice中添加数组列表中的项目,并将其加载到旋转器中
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/catNameSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown"
/>
spinner = findViewById(R.id.catNameSpinner);
ArrayList<String> cat = new ArrayList<>();
cat.add("Choose");
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>
(this,android.R.layout.simple_dropdown_item_1line,cat);
spinner.setAdapter(spinnerAdapter);
简单的/优雅的/我怎么做的:
预览:
XML:
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/btn_dropdown"
android:spinnerMode="dropdown"/>
spinnerMode设置为dropdown是android的下拉方式。(https://developer.android.com/reference/android/widget/Spinner attr_android: spinnerMode)
Java:
//get the spinner from the xml.
Spinner dropdown = findViewById(R.id.spinner1);
//create a list of items for the spinner.
String[] items = new String[]{"1", "2", "three"};
//create an adapter to describe how the items are displayed, adapters are used in several places in android.
//There are multiple variations of this, but this is the basic variant.
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);
//set the spinners adapter to the previously created one.
dropdown.setAdapter(adapter);
文档:
这是基础知识,但还有更多的东西可以通过实验自学。 https://developer.android.com/guide/topics/ui/controls/spinner.html
你可以使用setOnItemSelectedListener。(https://developer.android.com/guide/topics/ui/controls/spinner.html # SelectListener) 您可以从xml中添加字符串列表。(https://developer.android.com/guide/topics/ui/controls/spinner.html #填充) 这个视图有一个appCompat版本。(https://developer.android.com/reference/androidx/appcompat/widget/AppCompatSpinner)
转轮xml:
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
java:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Spinner spinner;
private static final String[] paths = {"item 1", "item 2", "item 3"};
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_spinner_item,paths);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
switch (position) {
case 0:
// Whatever you want to happen when the first item gets selected
break;
case 1:
// Whatever you want to happen when the second item gets selected
break;
case 2:
// Whatever you want to happen when the thrid item gets selected
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
这个代码对我来说很好,希望它也能帮助到你。
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="1dip" >
<TextView
android:id="@+id/spinnerItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="2dp"
android:textSize="12sp" >
</TextView>
</RelativeLayout>
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="20dp"
android:background="#FF00FF">
<Spinner
android:id="@+id/dropStatus"
android:layout_width="250dp"
android:layout_height="30dp"
android:layout_marginBottom="7dp"
android:drawSelectorOnTop="true"/>
</LinearLayout>
适配器类:
import java.util.ArrayList;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class StatusAdapter extends ArrayAdapter<StatusBean> {
private Context context;
private ArrayList<StatusBean> statuses;
public Resources res;
StatusBean currRowVal = null;
LayoutInflater inflater;
public StatusAdapter(Context context,
int textViewResourceId, ArrayList<StatusBean> statuses,
Resources resLocal) {
super(context, textViewResourceId, statuses);
this.context = context;
this.statuses = statuses;
this.res = resLocal;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.status_item, parent, false);
currRowVal = null;
currRowVal = (StatusBean) statuses.get(position);
TextView label = (TextView) row.findViewById(R.id.spinnerItem);
if (position == 0) {
label.setText("Please select status");
} else {
label.setText(currRowVal.getStatus());
}
return row;
}
}
StatusBean class:
public class StatusBean {
private String status;
private String statusCode;
public StatusBean() {
}
public StatusBean(String status,
String statusCode) {
this.status = status;
this.statusCode = statusCode;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusCode() {
return statusCode;
}
public void setStatusCode(String statusCode) {
this.statusCode = statusCode;
}
}
活动班级:
在onCreate方法内部:
static ArrayList<StatusBean> STATUS_LIST = new ArrayList<StatusBean>();
for(int i=0;i<=10;i++) {
STATUS_LIST.add(new StatusBean(“Status ”+i, “Stattus ”+i));
}
final Spinner dropStatus = (Spinner)findViewById(R.id.dropStatus);
Resources res = getResources();
StatusAdapter adapter = new StatusAdapter(this, R.layout.item, SessionData. STATUS_LIST, res);
dropStatus.setAdapter(adapter);
推荐文章
- 如何隐藏动作栏之前的活动被创建,然后再显示它?
- 是否有一种方法以编程方式滚动滚动视图到特定的编辑文本?
- 在Android中将字符串转换为Uri
- 数组与列表的性能
- 如何在NestedScrollView内使用RecyclerView ?
- 移动到另一个EditText时,软键盘下一步点击Android
- Android应用中的GridView VS GridLayout
- Activity和FragmentActivity的区别
- 右对齐文本在android TextView
- 为什么这个迭代的列表增长代码给出IndexError:列表赋值索引超出范围?如何重复添加(追加)元素到列表?
- 在Python中将列表初始化为特定长度
- 拆分字符串,在一行中转换为ist<int>()
- 权限拒绝:start前台需要android.permission.FOREGROUND_SERVICE
- 如何更改android操作栏的标题和图标
- Android Split字符串