问题描述
有没有办法通过编程是有一定的布局所有的孩子?
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);
为了禁用这两个textviews。
In order to disable the two textviews.
任何想法如何?
推荐答案
一个LinearLayout中延伸的ViewGroup,这样你就可以使用getChildCount()和getChildAt(指数)的方法来遍历您的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:
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.
}
这篇关于如何禁用/启用的Android上的LinearLayout所有儿童的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!