java - 如何在 Android 中禁用/启用 LinearLayout 上的所有子项

标签 java android android-layout android-linearlayout

有没有办法对某个布局的所有 child 进行编程?

例如,我有两个 child 的布局:

<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);

为了禁用两个 TextView 。

知道怎么做吗?

最佳答案

LinearLayout 扩展了 ViewGroup,因此您可以使用 getChildCount() 和 getChildAt(index) 方法来遍历您的 LinearLayout 子级,并对它们做任何您想做的事情。我不确定启用/禁用是什么意思,但如果你只是想隐藏它们,你可以做 setVisibility(View.GONE);

所以,它看起来像这样:

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.
}

关于java - 如何在 Android 中禁用/启用 LinearLayout 上的所有子项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7687784/

相关文章:

java - JSF 2 上的 HTTPS,用于 protected 资源和登录

java - 如何在Android Fragment中设置自动完成文本

java - 仅当我从 Playstore 下载时才会在应用程序中出现此登录问题

android - 在Android中使用GoogleHeaders时要包括哪些内容?

android - 如何给按钮加上边框和点击效果?

java - 在 Java 中,我如何将数字四舍五入到任意精度级别?

android - 如果街景在特定的纬度内不可用,如何获取最近的可用街景?

android-layout - 安卓工作室 |如何在 ToggleButton 中缩放图像?

android - 滚动条不会出现在具有多个图像的线性布局中

java - 解码json android