尝试将背景加载到LinearLayout时出现 NullpointerException 错误:
LinearLayout fondoSpinner =(LinearLayout)findViewById(R.id.fondoSpinner); fondoSpinner.setBackgroundResource(R.drawable.spinner_morado_gradiente);
LinearLayout属于自定义微调器。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fondoSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/spinner_naranja_gradiente"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imagenSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/textoSpinner"
style="@style/SpinnerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="left"/>
此代码还向您收取背景信息,它可以正常工作。当我尝试将其加载到main中时发生错误。我必须动态改变颜色
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.barra_titulo);
cargarCombo();
LinearLayout fondoSpinner = (LinearLayout)findViewById(R.id.fondoSpinner);
fondoSpinner.setBackgroundResource(R.drawable.spinner_morado_gradiente);
}
private void cargarCombo() {
Spinner spinner = (Spinner) findViewById(R.id.comboTipo_Vistos);
listaSpinner = new ArrayList<CountryInfo>();
listaSpinner = cargarComboVistos();
CountryAdapter myAdapter = new CountryAdapter
(this, android.R.layout.simple_spinner_item, listaSpinner);
spinner.setAdapter(myAdapter);
}
private ArrayList<CountryInfo> cargarComboVistos() {
ArrayList<CountryInfo> lista = new ArrayList<CountryInfo>();
int imagen = R.drawable.mapanaranja;
lista.add(new CountryInfo("Todos", imagen));
lista.add(new CountryInfo("Sí", imagen));
lista.add(new CountryInfo("No", imagen));
return lista;
}
public class CountryAdapter extends ArrayAdapter<CountryInfo>
{
private Activity context;
ArrayList<CountryInfo> data = null;
public CountryAdapter(Activity context, int resource, ArrayList<CountryInfo> data)
{
super(context, resource, data);
this.context = context;
this.data = data;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
return super.getView(position, convertView, parent);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // This view starts when we click the spinner.
View row = convertView;
if(row == null)
{
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.spinner_row, parent, false);
}
CountryInfo item = data.get(position);
if(item != null)
{ // Parse the data from each object and set it.
ImageView imagenSpinner = (ImageView) row.findViewById(R.id.imagenSpinner);
TextView textoSpinner = (TextView) row.findViewById(R.id.textoSpinner);
if(imagenSpinner != null)
{
imagenSpinner.setBackgroundResource(item.getCountryFlag());
}
if(textoSpinner != null)
textoSpinner.setText(item.getCountryName());
}
return row;
}
}
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<shape>
<solid
android:color="#9933CC" />
<stroke
android:width="3dp"
android:color="#AA66CC" />
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:startColor="#FFFFFF"
android:endColor="#FFFFFF"
android:angle="270"
android:type="linear"
/>
<stroke
android:width="1dp"
android:color="#9933CC" />
<padding
android:left="4dp"
android:top="4dp"
android:right="4dp"
android:bottom="4dp" />
</shape>
</item>
</selector>
答案 0 :(得分:0)
要自定义微调项目布局,请在适配器getView()
中执行此操作。例如:
@Override
public View getView(int position, View convertView, ViewGroup parent)
{ // Ordinary view in Spinner, we use android.R.layout.simple_spinner_item
View v = super.getView(position, convertView, parent);
v.setBackgroundResource(R.drawable.spinner_morado_gradiente);
return v;
}