我想custom Spinner Dialog
为我的应用程序创建。我找到了这个和这个。两者都很好。我已根据要求更新了我的代码。当用户点击微调器时,我正在使用选择器文件来更改对话框背景视图。
我的代码运行良好,但问题出在对话框视图中。它传播了那个宽度,但我想根据一些固定尺寸来获得。
我的 Spinner View 的当前输出如下:-
但我需要它如下: -
Spinner_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_popup" // it's my selector file
android:orientation="horizontal" >
<TextView
android:id="@+id/Genere_name"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginLeft="5dp"
android:ellipsize="marquee"
android:gravity="center_vertical"
android:singleLine="true"
android:textColor="@android:color/black"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
spinner_popup.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/popup_header" android:state_selected="true"/>
<item android:drawable="@android:color/transparent" android:state_enabled="true"/>
<item android:drawable="@drawable/popup_header" android:state_pressed="true"/>
<item android:drawable="@drawable/popup_bg_box" android:state_enabled="false" android:state_focused="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
Main_spinner_view
<LinearLayout
android:id="@+id/search_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/search_bg"
android:orientation="horizontal"
android:weightSum="3" >
<Spinner
android:id="@+id/Search_Spinner"
android:layout_width="95dp"
android:layout_height="30dp"
android:layout_gravity="left|center_vertical|center_horizontal"
android:layout_marginLeft="2dp"
android:layout_weight="1.2"
android:background="@drawable/search_dropdown" />
<EditText
android:id="@+id/Search_EDT"
android:layout_width="170dp"
android:layout_height="30dp"
android:layout_gravity="center|center_horizontal|center_horizontal"
android:layout_marginLeft="3dp"
android:layout_weight="1.5"
android:background="@drawable/search_textfield"
android:hint="Search By Free Text"
android:inputType="textPersonName"
android:padding="5dp"
android:textSize="16sp" >
</EditText>
<Button
android:id="@+id/Search_GO"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="right|center_horizontal|center_vertical"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="0.3"
android:background="@drawable/search_icon"
android:textColor="@android:color/white" />
</LinearLayout>
我在这里打电话给我的微调器适配器
Search_Spinner.setAdapter(new MyAdapter(GridViewActivity.this,
R.layout.spinner_item, Search_Category_List));
我认为java代码不是必需的,但我仍然粘贴在这里,
public class MyAdapter extends ArrayAdapter<String>
{
public MyAdapter(Context context, int textViewResourceId,
ArrayList<String> objects)
{
super(context, textViewResourceId, objects);
}
@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)
{
LayoutInflater inflater = getLayoutInflater();
View row = inflater.inflate(R.layout.spinner_item, parent, false);
TextView label = (TextView) row.findViewById(R.id.Genere_name);
spinner_text = Typeface.createFromAsset(getAssets(), "desc.ttf");
label.setTypeface(spinner_text);
label.setText(Search_Category_List.get(position));
return row;
}
}