【问题标题】:Table row add view takes up whole space表格行添加视图占用整个空间
【发布时间】: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


    【解决方案1】:

    您必须在要添加到行的View 上设置LayoutParams,而不是在TableRow 本身上设置。

    tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT, 1f));
    

    你会看到我添加了第三个参数,它对应于layout_weight。因此,如果您所有的 Views 的权重为 1,则您的 TableRowsumWeights 参数将自动设置为它们的总和,并且它们应该在您的行空间内平均分布宽度。

    【讨论】:

    • 它说你不能将 TableRow 布局参数添加到相对布局中,这是我的 View 添加到 TableLayout 的容器
    • 我更新了我的答案。您只需将TableRow.LayoutParams 替换为RelativeLayout.LayoutParams
    猜你喜欢
    • 1970-01-01
    • 2014-11-19
    • 1970-01-01
    • 2017-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-09-15
    • 2021-11-10
    • 2023-03-16
    相关资源
    最近更新 更多