提问人:Suwandi Cahyadi 提问时间:7/19/2015 更新时间:7/19/2015 访问量:107
RelativeLayout alignParentRight 点击后变得凌乱
RelativeLayout alignParentRight become messy after click
问:
我一直在尝试创建一个布局,在该布局中,我可以在 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);
}
}
}
这是正确的布局(在调用片段后首次显示):
这是不正确的布局(单击几次后):
它不会每次都发生,但经常发生,而且它只发生在我的真实设备中,而不是在模拟器中。我使用小米 4i Android 棒棒糖。
哪个部分错了?如何在不使用相对布局的情况下在父项的右侧显示视图?
谢谢
答: 暂无答案
评论