Android获取微调器来更改contentview

时间:2011-12-06 08:17:35

标签: android spinner

还有一个问题:/

这是我的微调代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >



<Spinner
    android:id="@+id/spinner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>

</LinearLayout>

我的班级:

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class mobs extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mobsspinner);

    Spinner s = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(
            this, R.array.mobsselect, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parentView, View     selectedItemView, int position, long id) {
            setContentView(R.layout.id);
        }

        @Override
        public void onNothingSelected(AdapterView<?> parentView) {
            // your code here
        }

    });



}

}

我希望能够将来自单独xml文件的内容与某些上下文放在一起。 内容应放在微调器下面。

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

如果我理解正确,你想在Spinner下面的代码中添加不同的视图吗?

如果是这种情况,您应该将ID添加到Spinner的LinearLayout父级,然后为新的布局/视图充气并将其附加到LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/linearlayout" >

<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>

</LinearLayout>

然后从你的代码

LinearLayout ll = (LinearLayout) findViewById(R.id.linearlayout);
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.mylayout, ll, false);
ll.addView(v);