【问题标题】:Android-XML custom spinnerAndroid-XML 自定义微调器
【发布时间】:2021-05-13 05:37:42
【问题描述】:

我正在开发一个 Android 应用程序,我有一个带有一些微调器和一个可搜索微调器的表单的片段。现在所有的微调器都有style="@android:style/Widget.Spinner.DropDown",使微调器像这样https://csharpcorner.azureedge.net/article/how-we-can-search-spinner-item-using-kotlin-in-android-studio/Images/8.png。 可搜索的微调器还有一个关闭按钮和像 https://csharpcorner.azureedge.net/article/how-we-can-search-spinner-item-using-kotlin-in-android-studio/Images/9.png 这样的自定义设计。现在我想更改设计,例如使微调器背景颜色为浅蓝色并更改文本“选择项目”和可搜索微调器内的“关闭”按钮的文本,但我不知道该怎么做。 有人能帮我吗? 一些微调器的 xml:

                android:id="@+id/customerSpinner"
                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/customerSpinner" />

            <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" /> ``

【问题讨论】:

    标签: java android xml kotlin


    【解决方案1】:

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

    这是一个例子

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

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

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

    在创建方法中找到您的 Spinner 并向其添加适配器

    // 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会从数组中取出数据,并像回收站视图一样设置到自定义视图中

    // 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 文件

    <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# 的图像及其标题

    您可以在这里设置背景颜色并根据您的需要更改

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

    这里会是这样的

    【讨论】:

    • 但我无法更改可搜索微调器的布局
    • 为此,我必须查看您的完整代码,但您可以参考Link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多