首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android-XML自定义微调器

Android-XML自定义微调器
EN

Stack Overflow用户
提问于 2021-02-09 23:03:14
回答 1查看 38关注 0票数 1

我正在开发一个Android应用程序,我有一个带有一些旋转器和一个可搜索旋转器的表单的片段。现在,所有的微调器都具有使微调器像这样的https://csharpcorner.azureedge.net/article/how-we-can-search-spinner-item-using-kotlin-in-android-studio/Images/8.pngstyle="@android:style/Widget.Spinner.DropDown"。searchable微调器还有一个close按钮和一个定制的设计,就像这样的https://csharpcorner.azureedge.net/article/how-we-can-search-spinner-item-using-kotlin-in-android-studio/Images/9.png。现在,我想要更改设计,例如,将微调器的背景颜色设置为浅蓝色,并更改文本"Select item“和"Close”按钮的文本,但我不知道如何做到这一点。有人能帮帮我吗?一些微调器的xml:

代码语言:javascript
复制
                android:id="@+id/costumerSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView6" />

            <TextView
                android:id="@+id/TextView7"
                style="@style/TitleStyle"
                android:text="@string/field_text"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/costumerSpinner" />

            <Spinner
                android:id="@+id/fieldSpinner"
                style="@android:style/Widget.Spinner.DropDown"
                android:layout_width="350dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/MarginStart20"
                android:layout_marginTop="5dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/TextView7" /> ``
EN

回答 1

Stack Overflow用户

发布于 2021-02-09 23:27:51

以下是我开始学习android时的尝试,希望能对你有所帮助

下面是一个例子

用你的可绘制文件声明一个这样的数据数组

您可以公开声明它,也可以在Oncrete方法中声明它

代码语言:javascript
复制
// Declaring the String Array with the Text Data for the Spinners

String[] Languages = { "Select a Language", "C# Language", "HTML Language",
"XML Language", "PHP Language" };

// Declaring the Integer Array with resource Id's of Images for the Spinners

Integer[] images = { 0, R.drawable.image_1, R.drawable.image_2,
R.drawable.image_3, R.drawable.image_4 };

在create方法中找到您的微调器,并为其添加一个适配器

代码语言:javascript
复制
// Declaring and typecasting a Spinner

Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
 
// Setting a Custom Adapter to the Spinner

mySpinner.setAdapter(new MyAdapter(MainActivity.this, R.layout.custom,
Languages));

现在,您的适配器将如下所示

在这里,Adapter将从数组中获取数据,并将其设置为自定义视图,就像回收器视图一样

代码语言:javascript
复制
// Creating an Adapter Class

public class MyAdapter extends ArrayAdapter {
 
public MyAdapter(Context context, int textViewResourceId,
String[] objects) {
super(context, textViewResourceId, objects);
}
 
public View getCustomView(int position, View convertView,
ViewGroup parent) {
 
// Inflating the layout for the custom Spinner

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom, parent, false);
 
// Declaring and Typecasting the textview in the inflated layout
TextView tvLanguage = (TextView) layout
.findViewById(R.id.tvLanguage);
 
// Setting the text using the array

tvLanguage.setText(Languages[position]);
 
// Setting the color of the text
tvLanguage.setTextColor(Color.rgb(75, 180, 225));
 
// Declaring and Typecasting the imageView in the inflated layout

ImageView img = (ImageView) layout.findViewById(R.id.imgLanguage);
 
// Setting an image using the id's in the array

img.setImageResource(images[position]);
 
// Setting Special attributes for 1st element

if (position == 0) {

// Removing the image view

img.setVisibility(View.GONE);

// Setting the size of the text

tvLanguage.setTextSize(20f);

// Setting the text Color

tvLanguage.setTextColor(Color.BLACK);
 
}
 
return layout;
}
 
// It gets a View that displays in the drop down popup the data at the specified position
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
 
// It gets a View that displays the data at the specified position
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
}

微调器或具有微调器的主活动的XML文件

代码语言:javascript
复制
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">

    <Spinner android:drawSelectorOnTop="true" android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:prompt="@string/select">
</LinearLayout>

创建文件名custom.xml

此XML文件用于显示图像和文本视图等视图

例如,这里有一张c#&的图片,它的标题是

您可以在此设置背景颜色,并根据需要进行更改

代码语言:javascript
复制
<LinearLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="3dip">
    <ImageView android:id="@+id/imgLanguage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher">
    </ImageView>
    <TextView android:id="@+id/tvLanguage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginTop="8dp" android:text="Text Here">
    </TextView>
</LinearLayout>

在这里,它看起来像这样

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66121540

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档