我创建了一个动态表。该表应该有一行,其中最左边是一个imageview,中间是textview,右边是一个复选框。所有控件都显示出来。 textview和复选框也是垂直居中的,但是inageview无法居中,因此行看起来很糟糕。 我想将imageview与textview和复选框放在一行。
上图显示了我现在得到的输出。我希望蓝色箭头与textview和复选框一致。
活动的源代码:
public class CalendarActivity extends Activity {
TableLayout first_calendar_table;
TableRow first_calendar_tr_data;
int tableRowCount = 0;
boolean showingFirst = true;
String chkChecked = "false";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calendar);
first_calendar_table = (TableLayout) findViewById(R.id.first_calendar_table);
tableHeads();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.calendar, menu);
return true;
}
// method to generate table heads
public void tableHeads() {
// ---------------Calendar Table
// Header-----------------------------------------------
TableRow supplier_details_tr_head = new TableRow(this);
supplier_details_tr_head.setId(10);
supplier_details_tr_head.setBackgroundResource(R.drawable.list_header);
supplier_details_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
// ImageView Setup
final ImageView imageView = new ImageView(this);
// setting image resource
imageView.setImageResource(R.drawable.closed_icon);
imageView.setId(20);
//imageView.setGravity(Gravity.CENTER);
// adding view to layout
supplier_details_tr_head.addView(imageView);
TextView select_head = new TextView(this);
select_head.setId(20);
select_head.setText(Html.fromHtml("2014-2015"));
select_head.setTextColor(Color.WHITE);
select_head.setGravity(Gravity.CENTER);
select_head.setPadding(5, 5, 5, 5);
supplier_details_tr_head.addView(select_head);// add the column to
// the
// table row here
select_head.setTextSize(20);
// select checkbox
final CheckBox customer_checkbox = new CheckBox(this);
customer_checkbox.setId(20);
customer_checkbox.setTextColor(Color.BLACK);
customer_checkbox.setGravity(Gravity.CENTER);
customer_checkbox.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
customer_checkbox.setButtonDrawable(R.drawable.checkbox_selector);
supplier_details_tr_head.addView(customer_checkbox);
first_calendar_table.addView(supplier_details_tr_head,
new TableLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
// ---------------Calendar Table
// Header-----------------------------------------------
// ----------------------On click imageView
imageView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(showingFirst == true){
imageView.setImageResource(R.drawable.opened_icon);
showingFirst = false;
}else{
imageView.setImageResource(R.drawable.closed_icon);
imageView.setTag(70);
showingFirst = true;
}
}
});
// ----------------------On click customer_name
}
// method to show toast message
public void makeAToast(String str) {
// yet to implement
Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="3dp"
tools:context=".CalendarActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
<RelativeLayout
android:id="@+id/CalendarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#FFFFFF" >
<TableLayout
android:id="@+id/first_calendar_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</TableLayout>
<TableLayout
android:id="@+id/second_calendar_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/first_calendar_table"
android:layout_centerHorizontal="true" >
</TableLayout>
<TableLayout
android:id="@+id/third_calendar_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/second_calendar_table"
android:layout_centerHorizontal="true" >
</TableLayout>
<TableLayout
android:id="@+id/fourth_calendar_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/third_calendar_table"
android:layout_centerHorizontal="true" >
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
我应该怎样做才能实现目标?