0
툴바에 문자열로 된 간단한 회 전자를 만들었습니다.이 항목이 string.xml에 필요하지 않습니다. 단지 자바 코드를 통해 항목을 추가하고 싶습니다. 선택 형태의 데이터베이스에서 가져온 다음 항목으로 동적으로 도구 모음 스피너에 항목을 추가 할이 당신은 아래와 같이 ArrayAdapter와 직접적으로 문자열의 배열을 추가 할 수 있습니다 내 활동 코드안드로이드에서 툴바 회 전자에 동적으로 항목을 추가하는 방법
package abtech.waiteriano.com.actionbartest;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private android.support.v7.widget.Toolbar toolbar;
private String[] category = {"hello", "this", "is", "me"};
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
category = getResources().getStringArray(R.array.category);
toolbar = (android.support.v7.widget.Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// toolbar.setLogo(R.mipmap.ic_launcher);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
navigationSpinner.setAdapter(spinnerAdapter);
toolbar.addView(navigationSpinner, 0);
navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,
"you selected: " + category[position],
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
답장을 보내 주셔서 감사합니다.하지만 여기에도 같은 문제가 있습니다. em은 private String [] category = { "hello", "this", "is", "me"}가 아닌 string.xml에 있습니다. – 7arooney
죄송하지만 지금은 잘 작동합니다. – 7arooney