[英]How to disable/enable all children on LinearLayout in Android
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. 为了禁用这两个textviews。
Any idea how? 知道怎么样?
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. LinearLayout扩展了ViewGroup,因此您可以使用getChildCount()和getChildAt(index)方法迭代LinearLayout子项并使用它们执行任何操作。 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);
我不确定启用/禁用你的意思,但如果你只是想隐藏它们,你可以做setVisibility(View.GONE);
So, it would look something like this: 所以,它看起来像这样:
LinearLayout myLayout = (LinearLayout) findViewById(R.id.linearLayout1);
for ( int i = 0; i < myLayout.getChildCount(); i++ ){
View view = myLayout.getChildAt(i);
view.setVisibility(View.GONE); // Or whatever you want to do with the view.
}
You can also disable/enable without using setVisibility() 您也可以在不使用setVisibility()的情况下禁用/启用
Add a View.OnClickListener to your CheckBox then pass the View you want to be disabled into the following function... 将View.OnClickListener添加到CheckBox,然后将要禁用的视图传递给以下函数...
private void enableDisableView(View view, boolean enabled) {
view.setEnabled(enabled);
if ( view instanceof ViewGroup ) {
ViewGroup group = (ViewGroup)view;
for ( int idx = 0 ; idx < group.getChildCount() ; idx++ ) {
enableDisableView(group.getChildAt(idx), enabled);
}
}
}
with the following reference Is there a way to disable all the items in a specific layout programmaticaly? 以下参考是否有办法禁用特定布局programmaticaly中的所有项目?
为什么不在布局本身上执行setVisibility(View.GONE),而不是遍历子节点?
Maybe is late, but you can just add android:duplicateParentState="true"
in the child of the LinearLayout
也许是迟到了,但你可以在
LinearLayout
的子LinearLayout
添加android:duplicateParentState="true"
Something like this: 像这样的东西:
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/textA"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:text="First Text"/>
<TextView
android:id="@+id/textB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:text="Second Text"/>
</LinearLayout>
只需添加另一个透明布局,该布局将匹配原始视图的match_parent,并在您要禁用所有子项时将其可见性更改为可见,并启用子项,然后将可见性更改为已消失
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.