죄송합니다. JS 및 CSS에서 8 개의 응용 프로그램을 작성하는 데 익숙합니다. 아직 Java에서는 좋지 않습니다.안드로이드에서 개별 listView 항목의 스타일을 지정하는 방법은 무엇입니까?
나는 첫 번째 안드로이드 앱 (노트 작성자)을 만드는 중입니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/notesListView"
android:fastScrollEnabled="true"
android:dividerHeight="2dp"
android:layout_alignParentStart="true"
android:divider="#b5b5ae"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
내 노트 즐긴다 응용 프로그램의 작동을 가지고 있지만 현재 UI가 s에 ** t : 나는 이런 식으로 내 XML에 정의 된 표준 목록보기 있습니다.
각 개별 목록보기 항목에 스타일을 적용하려고하지만 어디에서든지 할 수있는 멍청한 가이드를 찾을 수 없습니다.
목록에 추가 된 새 메모가 자동으로 미리 정의 된 스타일 (예 : 여백)을 갖기를 원합니다.
누구나 XML에서 각 개별 목록보기 항목에 스타일을 적용하는 가장 좋은 방법을 말해 줄 수 있습니까? 여백 설정 등.
또한 개별 목록보기 항목을 동적으로 변경하는 방법을 알고 있습니다 (사용자가 대화 상자에서 특정 색을 선택하면 개별 목록보기 항목이 BG 색을 변경하도록 만들 수 있기를 원합니다).
편집 1 :
안녕하세요, 여기 내 listNotes 활동에 대한 코드입니다 :
:package com.fishingfon.notetakerui;
public class ListNotesActivity extends Activity {
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
notes.remove(info.position);
populateList();
//populateLateCustomAdapter();
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_CANCELED){
return;
}
Serializable extra = data.getSerializableExtra("Note");
if (extra != null){
Note newNote = (Note)extra;
if (editingNoteId > -1){
notes.set(editingNoteId, newNote);
editingNoteId = -1;
}
else {
notes.add(newNote);
};
populateList();
//populateLateCustomAdapter();
}
}
private List<Note> notes = new ArrayList<Note>();
private ListView notesListView;
private int editingNoteId = -1;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_notes);
ListView notesListView = (ListView)findViewById(R.id.notesListView);
notesListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapter, View view, int itemNumber, long id) {
Intent editNoteIntent = new Intent(view.getContext(), EditNotesActivity.class);
editNoteIntent.putExtra("Note", notes.get(itemNumber));
editingNoteId = itemNumber;
startActivityForResult(editNoteIntent, 1);
}
});
registerForContextMenu(notesListView);
notes.add(new Note("1 Note", "blah blah", new Date()));
notes.add(new Note("2 Note", "blah blah", new Date()));
notes.add(new Note("3 Note", "blah blah", new Date()));
notes.add(new Note("4 Note", "blah blah", new Date()));
notes.add(new Note("5 Note", "blah blah", new Date()));
notes.add(new Note("6 Note", "blah blah", new Date()));
notes.add(new Note("7 Note", "blah blah", new Date()));
notes.add(new Note("8 Note", "blah blah", new Date()));
populateList();
//populateLateCustomAdapter();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.list_notes, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
//notes.add(new Note("Added note", "blah", new Date()));
//populateList();
Intent editNoteIntent = new Intent (this, EditNotesActivity.class);
startActivityForResult(editNoteIntent, 1);
return true;
}
// Populate Method
private void populateList() {
List<String> values = new ArrayList<String>();
for(Note note : notes) {
values.add(note.getTitle());
}
CustomListAdapter CustomAdapter = new CustomListAdapter();
notesListView.setAdapter(CustomAdapter);
}
을 그리고 여기에 당신이 내게 준 apadter 클래스이지만, 내 변수 이름 등으로
class CustomListAdapter extends BaseAdapter {
Context mContext;
List<String> mList;
public CustomListAdapter (Context context, List<String> values) {
mList = values;
mContext = context;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public String getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return 0;
}
// This method is called to draw each row of the list
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// here you inflate the layout you want for the row
final View view = View.inflate(mContext, R.layout.item_list, null);
return view;
}}
문제점은 populateList 메소드에 있습니다. 나는 위의 선을 굵게 만들었습니다. 새 어댑터를 작성하기 위해 populateList 메소드에 넣을 코드 및 매개 변수가 확실하지 않습니다.
내가 사용할 코드를 알고 있는지 궁금한 것이 있습니다. CustomListAdapter CustomAdapter = new CustomListAdapter();
및 전달할 매개 변수는 무엇입니까? 사전에
감사 힙
감사
건배 코리 B이 같은 메모 목록으로 item_list 자원을 결합 할 필요가 어댑터에
안녕하세요, 즉 완벽한 감사를 보인다. 나는 다만 1 개의 질문 tho가있다. 이 어댑터를 설정하는 구문은 무엇입니까?내가 사용한 이전 어댑터에서 : "ArrayAdapter 어댑터 = new ArrayAdapter (this, android.R.layout.simple_list_item_1, android.R.id.text1, values);". 하지만 당신이 준 어댑터에 대한 그 doesnt 작품. 내가 어댑터를 설정하는 데 사용할 줄 알아?. 다시 한번 감사드립니다. Corey " –
Fishingfon
사용자 지정 레이아웃을 목록에 바인딩 했습니까? 내 게시물을 업데이트했습니다. – Esteam
getView 메서드에서 convertView 매개 변수가 null인지 확인해야합니다. 그렇다면 새 레이아웃을 확장해야합니다. 그렇지 않은 경우 동일한 것을 다시 사용하십시오. 이것은 시스템 자원으로보다 경제적이며 훨씬 더 최적화 된 목록보기를 만듭니다. – Kuffs