的strings.xml
<string-array name="meal_array">
<item>Breakfast</item>
<item>Lunch</item>
<item>Dinner</item>
</string-array>
dietary.xml
<Spinner
android:id="@+id/ddl_meal_type"
android:layout_width="140dp"
android:layout_height="25dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:entries="@array/meal_array"
app:layout_constraintBottom_toBottomOf="@+id/lbl_meal_type"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.016"
app:layout_constraintStart_toEndOf="@+id/lbl_meal_type"
app:layout_constraintTop_toTopOf="@+id/lbl_meal_type"
app:layout_constraintVertical_bias="1.0"
tools:layout_conversion_absoluteHeight="24dp"
tools:layout_conversion_absoluteWidth="140dp" />
meal_layout.xml
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
android:src="@drawable/ic_action_food"
/>
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_gravity="center_vertical"
/>
Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
Spinner mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.meal_layout, R.id.txt, R.array.meal_array);
mySpinner.setAdapter(adapter);
return myView;
}
}
我想在微调器中的文本旁边添加图像。 Dietary.java中的代码似乎不起作用。我是用片段做的。 有人可以帮我吗?
答案 0 :(得分:0)
你的Dietary.java
public class Dietary extends Fragment{
View myView; //A view object called myView
int img[] = {0,R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round, R.mipmap.ic_launcher_round};
Spinner mySpinner;
SpinnerAdapter spinnerAdapter;
String[] mealArry = {"Select Meal","Breakfast", "Lunch", "Dinner"};
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.dietary, container, false);
mySpinner = (Spinner)myView.findViewById(R.id.ddl_meal_type);
spinnerAdapter = new SpinnerAdapter(mealArry, img, Main2Activity.this);
spinner.setAdapter(spinnerAdapter);
return myView;
}
}
您的meal_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".2"
android:padding="10dp" />
<TextView
android:id="@+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight=".8"
android:padding="10dp" />
</LinearLayout>
您的膳食文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/ddl_meal_type"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp" />
</LinearLayout>
现在,对于带有微调器的Text with Image,您需要创建一个自定义适配器。 为此,首先创建一个新的java文件,它将扩展BaseAdapter,baseAdapter有4个覆盖方法。
1。)
@Override
public int getCount() {
}
2)。
@Override
public Object getItem(int i) {
}
3。)
@Override
public long getItemId(int i) {
}
4。)
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
}
以下是你的SpinnerAdapter:
public class SpinnerAdapter extends BaseAdapter {
String[] mealPlan;
int[] mealPlanImage;
LayoutInflater layoutInflater;
Context context;
public SpinnerAdapter(String[] mealPlan, int[] mealPlanImage, Context context) {
this.mealPlan = mealPlan;
this.mealPlanImage = mealPlanImage;
this.context = context;
layoutInflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return mealPlan.length;
}
@Override
public Object getItem(int i) {
return mealPlan[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = layoutInflater.inflate(R.layout.meal_layout, null);
ImageView img = (ImageView) view.findViewById(R.id.img);
TextView txt = (TextView) view.findViewById(R.id.txt);
img.setImageResource(mealPlanImage[i]);
txt.setText(mealPlan[i]);
return view;
}
}
最后你的观点将如下图所示:
我希望你得到预期的结果。