我想要实现的是动态地向表行添加视图,而添加的视图的总宽度小于容器的总宽度。如果没有,请将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 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>
这是一个截图:
答案 0 :(得分:0)
您必须在LayoutParams
上设置View
,而不是TableRow
本身。
tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, 1f));
你会看到我添加了第三个参数,它对应于layout_weight
。因此,如果您的所有View
都具有权重1,则sumWeights
的{{1}}参数将自动设置为它们的总和,并且它们应在您的空间内均匀分布。行。