我试图在EditText框中用不同的(背景)颜色标记不同的行,即第1行 - 蓝色,第2行 - 黑色等。我通过在背景ListView中的相应文本框中填充颜色来实现此目的。我这样做是因为EditText没有提供只能设置一行背景颜色的方法(任何人请提供方法,如果你知道)。现在,每当用户滚动EditText框时,我也会以相同的数量滚动ListView,这样它们就会一起移动,感觉就像EditText框线条设置了相应的背景颜色。
我面临的唯一问题是当ListView向下/向上滚动时,显示的新行(在ListView中)(显示行下方的行)没有任何颜色组。似乎ListView没有绘制被揭示的新行(文本框)。这是因为ListView在EditText的背景中(我使用的是相对布局,下面是ListView,而上面是EditText)?是否有任何方法可以使ListView即使在滚动时也可以填充正确的颜色,以便显示新的线条?
编辑:所要求的代码: 这是layout.xml中的条目:
<RelativeLayout
android:id="@+id/container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<ListView android:id="@+id/bgLv1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:dividerHeight="0dp"
android:divider="@null"
/>
<EditText android:id="@+id/et1" android:layout_gravity="left" android:layout_height="match_parent" android:layout_width="match_parent" android:text="" android:gravity="center" android:background="@drawable/textbox_border"></EditText>
</RelativeLayout>
编辑2:发布ListAdapter代码: 这里b是布尔数组,在检查时,确定线的颜色。
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class BackgroundListAdapter extends ArrayAdapter<String>
{
private boolean b[];
private Context context;
BackgroundListAdapter(Context c, int a)
{
super(c,a);
context = c;
}
BackgroundListAdapter(Context c, int a, int b)
{
super(c,a,b);
context = c;
}
BackgroundListAdapter(Context c, int a, int b, List<String> l)
{
super(c,a,b,l);
context = c;
}
BackgroundListAdapter(Context c, int a, int b, String[] s)
{
super(c,a,b,s);
context = c;
}
BackgroundListAdapter(Context c, int a, List<String> l)
{
super(c,a,l);
context = c;
}
BackgroundListAdapter(Context c, int a, String[] s)
{
super(c,a,s);
context = c;
}
BackgroundListAdapter(Context c, boolean[] b)
{
super(c,R.layout.listview_background,new String[b.length]);
context = c;
this.b = b;
}
public View getView(int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
rowView = inflater.inflate(R.layout.listview_background, null);
rowView.setTag(new ViewHolder((TextView) rowView.findViewById(R.id.listBackgroundTV)));
}
((ViewHolder)rowView.getTag()).tv.setHeight((position != 0)?((TextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight():((TextBox)((Activity)context).findViewById(R.id.ctb1)).getLineHeight() + (int) (context.getResources().getDisplayMetrics().density + 0.5f));
((ViewHolder)rowView.getTag()).tv.setBackgroundColor((b[position])?0xff000000:0xffffffff);
return rowView;
}
}
class ViewHolder
{
public TextView tv;
ViewHolder(TextView tv)
{
this.tv = tv;
}
}
编辑3:onCreate方法:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et1 = (EditText)findViewById(R.id.et1);
et1.setText(inpStr);
final ListView lv1 = ((ListView)findViewById(R.id.bgLv1));
final BackgroundListAdapter bla = new BackgroundListAdapter(this,colorLines);
lv1.setAdapter(bla);
}
答案 0 :(得分:0)
这就是我为表格绘制矩形的方法
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape= "rectangle" >
<solid android:color="#FFFFFF"/>
<stroke android:width="1dp" android:color="#000000"/>
</shape>