将ImageView定义为右对齐

时间:2014-04-24 11:44:41

标签: android android-layout android-imageview

尝试将imageview与右对齐..代码崩溃。为什么?

更新:这是所有行有问题的。我想我在这里确定布局时失败了

    TableRow tableRow1 = new TableRow(this);
                tableRow1.setLayoutParams(new TableRow.LayoutParams(
                        TableRow.LayoutParams.MATCH_PARENT,
                        TableRow.LayoutParams.WRAP_CONTENT));

                tl.addView(tableRow1);

                /* textView1 */
                TextView textView1 = new TextView(this);
                textView1.setLayoutParams(new TableRow.LayoutParams(
                        LayoutParam

s.MATCH_PARENT, LayoutParams.MATCH_PARENT));
                textView1.setText(names);

                tableRow1.addView(textView1);
                ImageView icon;
                icon = new ImageView(this);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
                icon.setLayoutParams(params);

                // icon.setLayoutParams(params);
                icon.setImageResource(R.drawable.ic_done); //default value
                switch (value) {
                case "1":
                    icon.setImageResource(R.drawable.ic_done);
                    break;
                case "0":
                    icon.setImageResource(R.drawable.ic_bad);
                default:
                    break;
                }
                System.out.println("value : " + value);
                icon.setPadding(densityToPixels(13), 0, 0, 0);
                tableRow1.addView(icon);

2 个答案:

答案 0 :(得分:0)

尝试使用xml

<RelativeLayout
        android:id="@+id/beam_contact_entry_contact"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/beam_contact_entry_invite"
        android:background="?entry_background" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:orientation="vertical"
            android:layout_toRightOf="@+id/contact_info_avatar" >

            <TextView
                android:id="@+id/beam_contact_fragment_entry_text"
                style="?primary_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Alexander Great"
                android:textSize="18sp" />

            <TextView
                android:id="@+id/beam_contact_fragment_entry_text_number"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="-4dp"
                android:text="mobile"
                android:visibility="gone"
                style="?secondary_text"
                android:textSize="15sp" />


        </LinearLayout>

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/beam_contact_entry_invite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/beam_contact_entry_contact"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/beam_contact_entry_contact"
        android:background="?entry_background"
        android:orientation="horizontal" >   

        <ImageView
            android:id="@+id/beam_contact_fragment_entry_right_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:background="?entry_background"
            android:duplicateParentState="true"
            android:src="@drawable/sv_svyaznoy_contact" />

    </LinearLayout>

答案 1 :(得分:0)

首先,您动态定义了ImageView

ImageView icon = new ImageView(this);

然后你试图让LayoutParam通过icon.getLayoutParams();没有任何LayoutPram因为你没有设置任何LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)icon.getLayoutParams();

使其成功替换

TableRow.LayoutParams params = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
icon.setLayoutParams(params);

{{1}}