问题
I have created a listview called popuplistfragment.java and list_view.xml.I have added textview in xml file but I dont know where to add code to change the textview in the listview.
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class PopupListFragment extends ListFragment implements OnItemClickListener {
TextView textview;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.quotes, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
String item = (String) parent.getItemAtPosition(position);
Log.v("check", "item is clicked");
Intent myIntent = new Intent(getActivity(), SwipeViews.class);
myIntent.putExtra("key", item);
myIntent.putExtra("index", position);
startActivity(myIntent);
}
}
And this is the list_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:background="@drawable/back_listview">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
>
</ListView>
<TextView
android:id="@+id/text1"
android:textColor="#e15258"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
回答1:
Firstly you need to extend Adapter class
public class ListAdapter extends ArrayAdapter<String> {
String [] mQuotes;
public ListAdapter(String [] quotes){
mQuotes = quotes;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
v = layoutInflater.inflate(R.layout.itemlistrow, null);
}
TextView quoteTextView = v.findViewById(R.id.quote_textView);
quoteTextView.setText(mQuotes[position])
return v;
}
@Override
public int getCount() {
return quotes.length;
}
}
Then implement other methods. The next step is to change next code
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] QuoteArray = getResources().getStringArray(R.array.quotes);
ListAdapter adapter = new ListAdapter(QuoteArray);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
Then add itemlistrow.xml to Layout folder and change it so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/quote_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You can customize itemlistrow.xml as you want
来源:https://stackoverflow.com/questions/36086924/change-the-textview-style-in-androidstudio