【发布时间】:2014-02-10 14:26:17
【问题描述】:
我想要实现的是将视图动态添加到表格行,而添加的视图的总宽度小于容器的宽度。如果没有,请将TableRow 添加到TableLayout 并开始填充新行。每个视图都包含一个RelativeLayout,它有一个TextView 和一个ImageButton。问题是创建的TableRow 总是填满所有空间。
代码如下:
private void initializeTagWrapper() {
//temperery tags
mTags = new ArrayList<String>();
mTags.add("Catastrophe");
mTags.add("Another");
//find it
mTagWrapper = (TableLayout)findViewById(R.id.word_tag_wrapper);
//the display size
final Display display = getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final WindowManager.LayoutParams params = getWindow().getAttributes();
int totalWidth = 0;
TableRow row = new TableRow(getApplicationContext());
row.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT
));
//mtags are already converted.
if(mTags.size() != 0){
//we have n amount of tags created. loop through them and add children to the layout
for(int i = 0; i < mTags.size(); i++){
/**
* calculate the width of the screen
* take the dimension of the first element and add it to size of this row
* loop untill the size is greater than allowed, begin a new row
*/
//create the layout
View view = createView(i);
//check if this one will fit
if(totalWidth + view.getWidth() <= params.width * 0.95){
//allow the parent to display this child
row.addView(view);
totalWidth += view.getWidth();
}else{
//push the row into the layout and nullify it + reset total width
mTagWrapper.addView(row);
row = new TableRow(getApplicationContext());
totalWidth = 0;
}
}
}
}
private View createView(int position){
View view = getLayoutInflater().inflate(R.layout.word_tag, mTagWrapper);
//custom text view
CustomTextView tv = (CustomTextView)view.findViewById(R.id.word_tag_name);
//set the listener for the delete button
ImageButton button = (ImageButton)view.findViewById(R.id.word_tag_delete);
//set buttons click
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "deleted", Toast.LENGTH_SHORT).show();
}
});
//get the view's id
view.setId(1000 + position);
//set the textview text
tv.setText(mTags.get(position));
//return it back
return view;
}
这是View的xml
<?xml version="1.0" encoding="utf-8"?>
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/application_item_margin_small"
android:background="@drawable/tag"
android:layout_toRightOf="@+id/word_tag1"
android:id="@+id/word_tag">
<com.ivanvoynov.dictionary.AppUtils.CustomTextView
android:id="@+id/word_tag_name"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:text="Graph"
android:gravity="center"
android:layout_centerVertical="true"
app:customTypeface="@string/roboto_regular"
android:textColor="@color/application_red_color"
android:textSize="@dimen/font_size_smaller"/>
<ImageButton
android:layout_toRightOf="@+id/word_tag_name"
android:layout_marginLeft="@dimen/application_item_margin_medium"
android:layout_width="24dp"
android:layout_height="24dp"
android:id="@+id/word_tag_delete"
android:layout_centerVertical="true"
android:src="@drawable/ic_action_discard"
android:background="@drawable/split_action_contextual_selector"/>
</RelativeLayout>
控股TableLayout的xml是:
<TableLayout
android:id="@+id/word_tag_wrapper"
android:layout_below="@+id/word_tag_header"
android:layout_marginTop="@dimen/application_item_margin_medium"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</TableLayout>
这是一个截图:
【问题讨论】:
标签: android view android-relativelayout tablelayout tablerow