以编程方式设置ImageView样式无法正常工作

时间:2016-02-11 16:25:15

标签: android imageview styles tablelayout

我有TableLayout和ImageViews。

         <TableLayout
                android:id="@+id/tableLayout1"
                android:layout_height="match_parent"
                android:layout_width="wrap_content"
                >

                <TableRow
                    android:id="@+id/tableRow2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">


                    <ImageView
                        style="@style/image_view_style"
                        android:id="@+id/textView12"
                        android:background="@drawable/triangle"
                        android:src="@drawable/button_yes"
                        >
                    </ImageView>

                    <ImageView
                        style="@style/image_view_style"
                        android:src="@drawable/button_yes"
                        android:id="@+id/textView13">
                    </ImageView>


                    <ImageView
                        style="@style/image_view_style"
                        android:src="@drawable/button_yes"
                        android:id="@+id/textView14">
                    </ImageView>

                </TableRow>

                <TableRow
                    android:id="@+id/tableRow3"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >


                    <ImageView
                        android:id="@+id/textView21"
                        style="@style/image_view_style"
                        >
                    </ImageView>

                    <ImageView
                        android:id="@+id/textView22"
                        style="@style/image_view_style"
                        android:src="@drawable/button_no">
                    </ImageView>

                    <ImageView
                        android:id="@+id/textView23"
                        android:src="@drawable/button_yes"
                        style="@style/image_view_style">
                    </ImageView>
                </TableRow>

                <TableRow
                    android:id="@+id/tableRow4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" >


                    <ImageView
                        android:id="@+id/textView41"
                        style="@style/image_view_style"
                        android:cropToPadding="true"
                        android:src="@drawable/button_no">
                    </ImageView>

                    <ImageView
                        android:id="@+id/textView42"
                        style="@style/image_view_style"
                        android:src="@drawable/button_yes">
                    </ImageView>


                </TableRow>
            </TableLayout>

这是image_view_style.xml dimen / Image_view_width = 80和dimen / image_view_height = 80

<style name="image_view_style">
    <item name="android:layout_width">@dimen/image_view_width</item>
    <item name="android:layout_height">@dimen/image_view_height</item>
    <item name="android:padding">30dp</item>
    <item name="android:clickable">true</item>
</style>

当我运行应用程序时,它就可以了。app screen 但是,我得到了这个Tablelayout,并以编程方式设置它的内容。但是我最终获得的图像宽度和高度与之前的风格不匹配。 app screen after setting ImageView programatically

你能告诉我为什么以及如何解决它?

    TableLayout visitTableLayout = (TableLayout) getView().findViewById(R.id.tableLayout1);
   visitTableLayout.removeAllViewsInLayout();
                     ...
 TableRow tableRow = new TableRow(getContext());
        tableRow.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.WRAP_CONTENT));
                     ...
                        ImageView columnImageView = new ImageView(getContext());
                    columnImageView = (ImageView) getActivity().getLayoutInflater().inflate(R.layout.image_view_template_for_visit_table, null);
                    columnImageView.setImageResource(R.drawable.button_no);
                    columnImageView.setTag(R.string.column_tag, columsInThisRow);
                    columnImageView.setTag(R.string.student_tag, i);
                    tableRow.addView(columnImageView, columsInThisRow - 1);
                         ...
                     visitTableLayout.addView(tableRow, i);

image_view_style_for_visit_table.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/image_view_style"/>

0 个答案:

没有答案