RelativeLayout alignParentRight 点击后变得凌乱

RelativeLayout alignParentRight become messy after click

提问人:Suwandi Cahyadi 提问时间:7/19/2015 更新时间:7/19/2015 访问量:107

问:

我一直在尝试创建一个布局,在该布局中,我可以在 Android 中将文本放在父项的左侧,将 ImageView 放在父项的右侧。我使用 RelativeLayout 来实现这一点,这里是布局文件

         <TableLayout
                android:id="@+id/prop_displ_tbl_descr"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="7dp">
                <!-- Description -->
                <TableRow
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <RelativeLayout
                        android:id="@+id/prop_displ_rel_descr"
                        android:clickable="true"
                        android:layout_weight="1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">
                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="Deskripsi"
                            android:textStyle="bold"
                            android:layout_alignParentLeft="true"
                            android:layout_toLeftOf="@+id/prop_displ_img_collaps_descr"
                            android:textAppearance="?android:attr/textAppearanceMedium" />
                        <ImageView
                            android:id="@+id/prop_displ_img_collaps_descr"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="right"
                            android:layout_alignParentRight="true"
                            android:background="@drawable/ic_arrowdown" />
                    </RelativeLayout>
                </TableRow>

                <TableRow
                    android:id="@+id/prop_displ_tblrow_descr"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">
                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/prop_displ_txtdescription"
                        android:textAppearance="?android:attr/textAppearanceMedium" />
                </TableRow>
            </TableLayout>

第一次显示时布局正确。问题是因为我为 RelativeLayout 实现了一个 onTouchListener,以使 TableRow prop_displ_tblrow_descr可见性为 GONE,有时文本会消失,ImageView 将显示在左侧。

代码如下:

   relDescr.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            if(motionEvent.getAction() == MotionEvent.ACTION_UP) {
                mDescrIsExpanded = !mDescrIsExpanded;
                setCollapsibleDescr();

                // update ui
                relDescr.invalidate();
            }
            return false;
        }
    });

private void setCollapsibleDescr() {
    if(mTblRowDescr != null) {
        if(mDescrIsExpanded) {
            // view is expanded
            mTblRowDescr.setVisibility(View.VISIBLE);
            mImgDescr.setBackgroundResource(R.drawable.ic_arrowup);
        } else {
            // view is collapsed
            mTblRowDescr.setVisibility(View.GONE);
            mImgDescr.setBackgroundResource(R.drawable.ic_arrowdown);
        }
    }
}

这是正确的布局(在调用片段后首次显示):correct layout at first display

这是不正确的布局(单击几次后):incorrecy layout after click some times

它不会每次都发生,但经常发生,而且它只发生在我的真实设备中,而不是在模拟器中。我使用小米 4i Android 棒棒糖。

哪个部分错了?如何在不使用相对布局的情况下在父项的右侧显示视图?

谢谢

点击 android-relativelayout

评论

0赞 Gil Vegliach 7/19/2015
尝试切换 TextView 和 ImageView:先将 ImageView 放在首位,然后再将 TextView
0赞 y7876768 7/19/2015
你试过使用LinearLayout吗?
0赞 Suwandi Cahyadi 7/20/2015
嗨,如何使用 LinearLayout 实现位于父级右侧的视图?

答: 暂无答案