已修改编辑:我已解决此问题。它实际上没有逻辑意义,但除了复选框之外的所有内容都必须是“Match_Parent”#39;对于宽度和高度,不是' Wrap_Content' (看起来很奇怪)。
我在ListView切断文本中遇到自定义ArrayAdapter的问题。
一张图片胜过千言万语,所以这里是问题的图片:
正如你所看到的那样,第四条线正在被切断,在“裂缝”上。
以下是自定义适配器的代码:
public class CheckListAdapter extends ArrayAdapter<String>{
private int rowPadding = (int) getContext().getResources().getDimensionPixelSize(R.dimen.adapter_mediumRowVPadding);
private int medText = (int) getContext().getResources().getDimensionPixelSize(R.dimen.medium_text);
private Map<String,Boolean> map = new HashMap<String, Boolean>();
private List<String> content;
// Strings incoming are converted to a LinkedHashSet before being used.
// This ensures that the adapter content will not accidentally change,
// and that there is only one copy of a given string per adapter.
// A LinkedHashSet is a HashSet with a predictable iteration order.
public CheckListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, new ArrayList<String>(new LinkedHashSet<String>(objects)));
}
public CheckListAdapter(Context context, List<String> objects) {
this(context, android.R.layout.simple_list_item_1, objects);
}
public CheckListAdapter(Context context, String[] objects) {
this(context, android.R.layout.simple_list_item_1, Arrays.asList(objects));
}
@Override
public View getView(final int position, View convertView, ViewGroup parent){
LinearLayout row = new LinearLayout(this.getContext());
row.setBackgroundResource(R.drawable.greenclicked_background);
row.setLayoutParams(new AbsListView.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
final CheckBox check = new CheckBox(getContext());
check.setButtonDrawable(R.drawable.gemsgreen_btn_check_holo_light);
final TextView text = new TextView(getContext());
LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, Gravity.FILL);
text.setLayoutParams(params);
text.setGravity(Gravity.TOP | Gravity.LEFT);
final String item = this.getItem(position);
if(!this.map.containsKey(item)){
this.map.put(item, false);
}
else{
boolean checked = map.get(item);
check.setChecked(checked);
}
row.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
boolean checked = map.get(item);
checked = !checked;
map.put(item, checked);
check.setChecked(checked);
}
});
text.setText(item);
text.setMinLines(1);
text.setMaxLines(10);
row.addView(check);
row.addView(text);
row.setPadding(0, rowPadding, 0, rowPadding);
return row;
}
public Map<String,Boolean> getResults(){
return map;
}
}
这是自定义ListView的代码:
public class NonScrollListView extends ListView {
public NonScrollListView(Context context) {
super(context);
}
public NonScrollListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public NonScrollListView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightMeasureSpec_custom = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec_custom);
ViewGroup.LayoutParams params = getLayoutParams();
params.height = getMeasuredHeight();
}
}
我一直在寻找这个问题的解决方案近一个小时了,我绝对没有运气。我尝试了许多使用不同重力和布局参数设置的不同方法,但没有任何方法使它看起来与截图中的外观有任何不同。
答案 0 :(得分:0)
我认为你应该用xml定义每一行布局。 例如: row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp" />
</LinearLayout>
在 getView()方法中,您实现如下:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
// If holder not exist then locate all view from UI file.
if (convertView == null) {
// inflate UI from XML file
convertView = inflater.inflate(R.layout.home_page_listview_items,
parent, false);
// get all UI view
holder = new ViewHolder(convertView);
// set tag for holder
convertView.setTag(holder);
} else {
// if holder created, get tag from view
holder = (ViewHolder) convertView.getTag();
}
//continue your code here
}
private static class ViewHolder {
private Checkbox check;
private TextView text;
public ViewHolder(View view) {
check = (ImageView) view.findViewById(R.id.check);
text = (TextView) view.findViewById(R.id.text);
}
}
希望这有帮助!