Android 当视图可见性为 View.GONE 时,RelativeLayout 出现问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3279081/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Issue with RelativeLayout when View visibility is View.GONE
提问by James
I've a RelativeLayout
thus:
我有一个RelativeLayout
这样的:
<RelativeLayout>
<TextView1/>
<TextView2/> // <-- View.VISIBLE OR View.GONE
<TextView3/>
<TextView4/>
</RelativeLayout>
Each TextView
is anchored below the previous TextView
with android:layout_below
.
每个TextView
锚定跌破前期TextView
用android:layout_below
。
The problem is that TextView2 may or may not be there (either View.VISIBLE
or View.GONE
); if it's View.VISIBLE
, then all is fine, but if it's View.GONE
, then TextView3 ends up being rendered on top of TextView1.
问题是 TextView2 可能存在也可能不存在(或者View.VISIBLE
或View.GONE
);如果是View.VISIBLE
,则一切正常,但如果是View.GONE
,则 TextView3 最终会呈现在 TextView1 之上。
I've tried various ways to fix this, but each time am caught out by RelativeLayout
's 'you cannot reference an id before it's defined' rule.
我尝试了各种方法来解决这个问题,但每次都被RelativeLayout
“你不能在定义之前引用 id”规则所困扰。
I'm hoping that I'm missing something obvious here.
我希望我在这里遗漏了一些明显的东西。
采纳答案by Karan
You can place textview 2 and 3 in the LinearLayout and keep the linear layout below textview 1.
您可以将 textview 2 和 3 放在 LinearLayout 中,并将线性布局保持在 textview 1 下方。
回答by ininprsr
You can use this tag:
你可以使用这个标签:
android:layout_alignWithParentIfMissing="true"
From the docs:
从文档:
If set to true, the parent will be used as the anchor when the anchor cannot be be found for layout_toLeftOf, layout_toRightOf, etc.
如果设置为 true,则在 layout_toLeftOf、layout_toRightOf 等无法找到锚点时,将使用父级作为锚点。
回答by slup
why not update the below
attribute of TextView3 when you update the visibility of TextView2? (I assume you do this in code)
更新below
TextView2的可见性时,为什么不更新TextView3的属性?(我假设您在代码中执行此操作)
something like
就像是
TextView tv = (TextView) findViewById(R.id.textview3);
RelativeLayout.LayoutParams lp =
(RelativeLayout.LayoutParams) tv.getLayoutParams();
lp.addRule(RelativeLayout.BELOW, R.id.textview1);
((TextView) view).setLayoutParams(lp);
回答by Samuel Yang
This answer does not solve your specific problem, but does solve a similar one, so hopefully this will help somebody.
这个答案不能解决您的具体问题,但确实解决了类似的问题,所以希望这会对某人有所帮助。
I had a situation where my relative layout did not have the equivalent of your TextView1. So, in my situation, if TextView2 was GONE, then I wanted TextView3 to be aligned with the parent's top. I solved that by adding to TextView3 the attribute android:layout_alignWithParentIfMissing="true". See http://developer.android.com/resources/articles/layout-tricks-efficiency.html.
我遇到过这样一种情况,我的相对布局没有与 TextView1 等效的布局。所以,在我的情况下,如果 TextView2 消失了,那么我希望 TextView3 与父级的顶部对齐。我通过向 TextView3 添加属性 android:layout_alignWithParentIfMissing="true" 解决了这个问题。请参阅http://developer.android.com/resources/articles/layout-tricks-efficiency.html。
Unfortunately, I do not see a way to specify an alternate alignment anchor unless it is the parent.
不幸的是,除非它是父级,否则我看不到指定替代对齐锚点的方法。
回答by user925799
Forget about INVISIBLE
or GONE
, use this instead:
忘记INVISIBLE
or GONE
,改用这个:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) view.getLayoutParams();
params.height = 0;
params.setMargins(0,0,0,0);
view.setLayoutParams(params);
回答by Alittle927
you can do this
你可以这样做
<RelativeLayout>
<TextView1/>
<FrameLayout>
<TextView2/> // <-- View.VISIBLE OR View.GONE
</FrameLayout>
<TextView3/>
<TextView4/>
</RelativeLayout>
let TextView3 below this FrameLayout which has no background, so if TextView2 is Gone ,it doesn't occupy space.
让 TextView3 在这个没有背景的 FrameLayout 下方,所以如果 TextView2 消失了,它不会占用空间。
回答by Ashwini
place all textViews under LinearLayout with vertical orientation.
将所有 textViews 放置在 LinearLayout 下,垂直方向。
<LinearLayout>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
<TextView/>
</LinearLayout>
回答by Taldroid
A simple hack for this is to play with alpha 0/1. and also disable the onClickListener if there is any
一个简单的技巧是使用 alpha 0/1。并且如果有的话也禁用 onClickListener