安卓灰色线性布局问题

5

我有一个线性布局,其中包含一组子视图(文本、图像和其他布局的混合物)。

我想将线性布局和所有子视图变灰,使它们看起来被禁用(类似于按钮的工作方式)。

这个目标是否能够实现?我知道在画布上绘制时可以设置颜色过滤器,是否有类似于布局的功能呢?


不能只设置背景颜色吗? - John Joe
不行,因为所有的文本/子视图都叠加在背景颜色上面。所以它们看起来不会被禁用。 - Michael Edwards
3个回答

0

您可以使用Frame-layout

使用TEST ONE,您可以实现上述文本视图的禁用效果。您可以将根布局的可点击性设置为false,以实际禁用布局内的视图。 TEST TWO是为了理解目的。例如,您需要管理视图的顺序以显示效果。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="match_parent">
<!--THIS IS TEST ONE-->
<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="150dp"
    android:background="#000000"
    android:gravity="center"
    android:text="TEST DATA"
    android:textColor="@android:color/white"
    android:textSize="30sp" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:background="#DAD6D6D6"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data"
        android:textColor="@color/colorAccent"
        android:textSize="30sp" />

</LinearLayout>

<!--THIS IS TEST TWO-->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:layout_marginTop="300dp"
    android:background="#DAD6D6D6"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Data"
        android:textColor="@color/colorAccent"
        android:textSize="30sp" />

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="400dp"
    android:background="#000000"
    android:gravity="center"
    android:text="TEST DATA"
    android:textColor="@android:color/white"
    android:textSize="30dp" />
</FrameLayout>

0
为了实现您的布局,您可以使用RelativeLayout。在RelativeLayout中,您可以使用两个不同的视图或LinearLayout。在第一个布局中,您可以根据需要添加视图,在第二个布局中,您可以设置alpha颜色代码以禁用布局。
请尝试以下代码:
<RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/demo_img_1" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/demo_img_1"
                android:layout_marginLeft="20dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/trans"/>

    </RelativeLayout>

以下是代码的输出:

enter image description here


0

1
我尝试过这个,但是其中一些子视图没有自动“灰化”自己。 - Michael Edwards
@MichaelEdwards 请尝试改进的解决方案 https://dev59.com/sWw05IYBdhLWcg3wuUGY#49364466 - TooTall

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接