如何在 Android 中禁用/启用 LinearLayout 上的所有子项
有没有办法通过编程来确定某个布局的所有子项?
例如,我有两个孩子的布局:
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:layout_width="fill_parent">
<SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
android:layout_weight="1" android:layout_width="fill_parent"></SeekBar>
<TextView android:id="@+id/textView2" android:text="TextView"
android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
并且我想做类似的事情:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setEnabled(false);
为了禁用两个文本视图。
知道怎么做吗?
Is there way by programming that all the children of a certain layout?
For example i have this layout with two children:
<LinearLayout android:layout_height="wrap_content"
android:id="@+id/linearLayout1" android:layout_width="fill_parent">
<SeekBar android:layout_height="wrap_content" android:id="@+id/seekBar1"
android:layout_weight="1" android:layout_width="fill_parent"></SeekBar>
<TextView android:id="@+id/textView2" android:text="TextView"
android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
and i want to do something like:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
myLayout.setEnabled(false);
In order to disable the two textviews.
Any idea how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
LinearLayout 扩展了 ViewGroup,因此您可以使用 getChildCount() 和 getChildAt(index) 方法来迭代 LinearLayout 子级并对它们执行任何您想要的操作。我不确定启用/禁用是什么意思,但如果您只是想隐藏它们,您可以执行 setVisibility(View.GONE);
所以,它看起来像这样:
A LinearLayout extends ViewGroup, so you can use the getChildCount() and getChildAt(index) methods to iterate through your LinearLayout children and do whatever you want with them. I'm not sure what you mean by enable/disable, but if you're just trying to hide them, you can do setVisibility(View.GONE);
So, it would look something like this:
您还可以在不使用 setVisibility() 的情况下禁用/启用
将 View.OnClickListener 添加到您的 CheckBox,然后将要禁用的视图传递到以下函数中...
使用以下参考 有吗一种以编程方式禁用特定布局中所有项目的方法?
You can also disable/enable without using setVisibility()
Add a View.OnClickListener to your CheckBox then pass the View you want to be disabled into the following function...
with the following reference Is there a way to disable all the items in a specific layout programmaticaly?
为什么不只在布局本身上执行 setVisibility(View.GONE) ,而不是迭代子级?
Why not just doing the setVisibility(View.GONE) on the layout itself, rather than iterating through the children?
也许已经晚了,但您可以在
LinearLayout
的子级中添加android:duplicateParentState="true"
,如下所示:
Maybe is late, but you can just add
android:duplicateParentState="true"
in the child of theLinearLayout
Something like this:
只需添加另一个透明布局,该布局将与原始视图的 match_parent 并在您想要禁用所有子视图并启用子视图时将其可见性更改为可见,然后只需将可见性更改为消失即可
Just add another transparent layout that will match_parent of the original view and change it visibility to visible when you want to disable all the children and to enable children then just change visibility to gone