我怎样才能样式的Andr​​oid微调提示样式、提示、Andr、oid

2023-09-06 19:06:30 作者:﹌生活过得像句费话°

我已经开发了一个例子。

I have developed one example.

在这里,我要的风格旋转器提示。请帮我。我怎样才能风格呢?

Here I have to style a spinner prompt. Please help me. How can I style it?

这是我的 string.xml code:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, CustomizedListView!</string>
<string name="app_name">CustomizedListView</string>
<string name="status_prompt">Choose a Status</string>
</resources>

这code用在我的main.xml:

This code is used on my main.xml:

<Spinner android:id="@+id/spinner1" 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_x="106dp"
    android:layout_y="100dp"
    android:prompt="@string/status_prompt" 
    android:background="@drawable/btn_dropdown"/>

在这里,我想改变背景颜色和 TEXTSIZE TEXTSTYLE 下面的图像。我怎样才能改变这一点。

Here I wish to change the background color and textSize, textStyle on below image. How can I change this.

编辑: 我已经加入了跌破code。在 styles.xml

I have added the below code in styles.xml

<style name="spinner_style">                        
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#040404</item>
<item name="android:typeface">monospace</item>
<item name="android:background">#FFFFFF</item>
</style>

还增加了以下线的main.xml中进行微调:

also added below line on the main.xml for spinner:

  style="@style/spinner_style"   

但是,它也没有为我工作。我怎样才能样式的微调提示消息。

But it also did not work for me. How can I style the spinner prompt message.

编辑:

这是我的Java $ C $下 ArrayAdaper

This is my java code for ArrayAdaper:

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, list);
    //set the view for the Drop down list
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    //set the ArrayAdapter to the spinner
    spinner.setAdapter(adapter);
    adapter.notifyDataSetChanged();
    //attach the listener to the spinner
    spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

    }

我怎么可以在这里设置提示风格。

How can I set the prompt style here.

推荐答案

为了改变提示,您需要使用类似的方法是什么// @ aswin - 库马尔建议,但有一个单独的样式的第一个元素下拉:

In order to change the prompt, you need to use a similar method to what @aswin-kumar suggested, but with a separate style for the first element of the drop down:

首先,延长 ArrayAdapter 并称之为 CustomArrayAdapter : 包com.example.project;

First, extend ArrayAdapter and call it CustomArrayAdapter: package com.example.project;

package com.example.project;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.List;


// I extend this using ArrayAdapter<String>, but you can use anything as the type
// parameter, or parameterize it as necessary.
public class CustomArrayAdapter extends ArrayAdapter<String> {

    private String title;

    public CustomArrayAdapter(Context aContext, int aTextViewResource, List<String> aOptions, String title) {
        super(aContext, aTextViewResource, aOptions);
        this.title = title;
    }

    @Override
    public View getDropDownView(int aPosition, View aConvertView, ViewGroup aParent) {

        LinearLayout v = null;

        if (aPosition == 0) {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (LinearLayout) inflater.inflate(R.layout.dropdown_header, aParent, false);
            TextView tv = (TextView) v.findViewById(R.id.dropdown_item);
            tv.setText(getItem(aPosition));
        } else {
            LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = (LinearLayout) inflater.inflate(R.layout.dropdown_item, aParent, false);
            TextView tv = (TextView) v.findViewById(R.id.dropdown_item);
            tv.setText(getItem(aPosition));
            tv.setHeight((int) (tv.getTextSize() * 2));
        }


        return v;
    }

    @Override
    public int getCount() {
        return super.getCount() + 1;
    }

    @Override
    public String getItem(int position) {
        if (position == 0) {
            return title;
        }
        return super.getItem(position - 1);
    }

    @Override
    public boolean isEnabled(int position) {
        return position != 0;
    }
}

现在,请使用以下布局的 dropdown_header.xml

Now, use the following layout for your dropdown_header.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/dropdown_item"
        android:textColor="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="16dp"
        android:paddingBottom="16dp"
        android:paddingLeft="16dp"
        android:gravity="left|center_vertical"
        android:textSize="9pt" />

    <!-- This adds a black separator line between the title and the items. You can remove
         if you want -->
    <LinearLayout
        android:id="@+id/separator"
        android:layout_height="4dp"
        android:layout_width="match_parent"
        android:background="@android:color/black"
        android:orientation="vertical"/>
    </LinearLayout>

在表单视图布局文件(form_view_layout.xml):

The form view layout file (form_view_layout.xml):

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinner_dialog_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="9pt" />

微调器布局文件( spinner_layout.xml ):

 <Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="@android:color/transparent"/>

现在,装上新的适配器将微调

Now, attach the new adapter to your Spinner:

CustomArrayAdapter adapter = new CustomArrayAdapter(aActivity, R.layout.form_view_layout, aModel.getEnumerableOptions(), aModel.getTitle());
View parentView = aActivity.getLayoutInflater().inflate(R.layout.spinner_layout, aParent, false);

spinner = (Spinner) parentView.findViewById(R.id.spinner);
spinner.setAdapter(adapter);

最后,添加要显示你的微调适配器,适配器的字符串,风格相应的文件,你应该是好去。

Finally, add the strings you want to display to your spinner adapter, adapter, and style the appropriate files, and you should be good to go.