我在设置列表项布局的重力时遇到问题。我有一个包含 2 个文本视图的列表布局。第一个有右重力,第二个有左重力。当我加载列表视图时,我总是得到正确的重力。
这是我的代码:
<?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="vertical" >
<TextView
android:id="@+id/poem_verse_list_item_01"
style="@style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" />
<TextView
android:id="@+id/poem_verse_list_item_02"
style="@style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left" />
</LinearLayout>
这是我的光标及其 ViewBinder:
public class CustomViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int getIndex = cursor.getColumnIndex(ClubCP.COLUMN_POSITION);
int position = cursor.getInt(getIndex);
// Log.i("ViewBinder position",position);
TextView right =(TextView)view;
int viewId = view.getId();
switch(position){
case 0 :
// Right = 0,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.RIGHT);
// text.setVisibility(text.GONE);
// return true;
case 1:
// Left = 1,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.LEFT);
// return true;
case 2:
case 3:
case 4:
case -1:
}
return false;
}
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.poem_verse_list_item, c,
columns, new int[] {R.id.poem_verse_list_item_01,R.id.poem_verse_list_item_02}, 0);
我测试了很多,但每次我都只得到左右重力......我希望有人能帮助我。谢谢你的时间。
这是我的活动,包括 ListView 布局:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff64512f"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".Read_peom" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/list_top_repeat" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scaleType="fitXY"
android:src="@drawable/list_bottom_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="@drawable/list_left_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="@drawable/list_right_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/list_top_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/list_top_right_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="@drawable/list_bottom_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="@drawable/list_bottom_right_no_repeat" />
<TextView
android:id="@+id/txt_poem_container_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="@+id/list_poem_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_margin="@dimen/list_top_margin"
android:choiceMode="none"
android:clickable="false"
android:longClickable="false"
android:padding="5dp" >
</ListView>