【问题标题】:setVisibility(view.GONE) is not working AndroidsetVisibility(view.GONE) 无法正常工作
【发布时间】:2015-09-10 11:41:48
【问题描述】:

我需要以编程方式禁用视图,因此我在活动中使用了 setVisibility(view.GONE)。我的 xml 有两个名为lower 和upper 的按钮以及两个名为lower_lay 和upper_lay 的相对布局。当我单击下按钮时,我需要启用lower_lay,单击上按钮时,我需要启用upper_lay。在 upeer_lay 和 lower_lay 中都有图像并执行 onTouch 事件。现在我的问题是,当我在 lower_lay 中时,upper_lay 的图像被禁用,但是当我触摸 lower_lay 中的空白空间时,upper_lay 图像即将出现……而在 upper_lay 中,我遇到了这个问题。为什么视图没有完全消失?

我从 3 天开始尝试这个....请任何人帮助我。

xml:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/r1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"

  >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"

        android:fillViewport="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

        <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#1e356a">

        <TextView
            android:id="@+id/placce_head"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="10dp"
            android:text="Hyderabad to banglore"
            android:textColor="#ffffff"
            android:textSize="20dp" />



    </RelativeLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#78869c"
                android:weightSum="2"

                android:orientation="horizontal">


            <TextView
                android:id="@+id/seats"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="10dp"
                android:text="Selected Seats"
                android:textColor="#ffffff"
                android:textSize="16dp" />
                <TextView
                    android:id="@+id/totalamount"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"
                    android:text="Total Amount"
                    android:textColor="#ffffff"
                    android:textSize="16dp" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#78869c"
                android:weightSum="2"
                android:orientation="horizontal">


                <TextView
                    android:id="@+id/seat_num"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"
                    android:layout_marginLeft="10dp"

                    android:textColor="#ffffff"
                    android:textSize="16dp" />
                <TextView
                    android:id="@+id/total_amount"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:layout_marginLeft="10dp"
                    android:layout_centerHorizontal="true"
                    android:layout_centerVertical="true"


                    android:textColor="#ffffff"
                    android:textSize="16dp" />
        </LinearLayout>
           <LinearLayout
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:orientation="vertical"
               android:background="#1e356a"
              >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#1e356a"
                android:layout_marginTop="5dp"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:src="@drawable/bluesmall"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:text="Available"
                    android:textSize="12dp"
                    android:textColor="#ffffff"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="5dp"
                    android:src="@drawable/greensmall"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:textSize="12dp"
                    android:textColor="#ffffff"
                    android:text="Selected"/>
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"

                    android:src="@drawable/pinksmall"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Ladies"
                    android:textSize="12dp"
                    android:textColor="#ffffff"
                    android:layout_marginLeft="6dp"/>
                <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                android:src="@drawable/redsmall"/>
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:textSize="12dp"
                    android:textColor="#ffffff"
                    android:text="Booked"/>


                </LinearLayout>
               <LinearLayout
                   android:id="@+id/sleeper_lay"
                   android:layout_width="match_parent"
                   android:layout_height="wrap_content"
                   android:orientation="horizontal"
                   android:background="#78869c"
                   android:weightSum="2"

                   android:padding="7dp"
                   android:layout_marginTop="10dp"
                   android:layout_marginLeft="10dp"
                   android:layout_marginRight="10dp"
                   >
                   <Button
                       android:id="@+id/lower"
                       android:layout_width="0dp"
                       android:layout_weight="1"
                       android:text="Lower"
                       android:textColor="#F93249"
                       android:gravity="center"
                       android:textSize="18dp"
                       android:layout_height="wrap_content" />
                   <Button
                       android:id="@+id/upper"
                       android:layout_width="0dp"
                       android:layout_weight="1"
                       android:text="Upper"
                       android:textSize="18dp"
                       android:textColor="#ffffff"
                       android:gravity="center"
                       android:layout_height="wrap_content" />

               </LinearLayout>





            <RelativeLayout
        android:id="@+id/relative_layout"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/bg_border"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center" >






    </RelativeLayout>
               <RelativeLayout
                   android:id="@+id/relative_layout_two"

                   android:layout_marginLeft="10dp"
                   android:layout_marginRight="10dp"
                   android:layout_marginBottom="10dp"
                   android:background="@drawable/bg_border"
                   android:visibility="gone"
                   android:layout_width="match_parent"
                   android:layout_height="match_parent"
                   android:gravity="center" >






               </RelativeLayout>

<Button
    android:id="@+id/done_btn"
    android:background="#F93249"
    android:textColor="#ffffff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="done"/>

           </LinearLayout>

        </LinearLayout>
    </ScrollView>




</RelativeLayout>

活动:

lower_lay = (RelativeLayout) findViewById(R.id.relative_layout);//lower layout
       upper_lay= (RelativeLayout) findViewById(R.id.relative_layout_two);//upperlayout
    upper.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

           lower_lay.setVisibility(View.GONE);
            upper.setTextColor(Color.parseColor("#F93249"));
            lower.setTextColor(Color.parseColor("#ffffff"));
            upper_lay.setVisibility(View.VISIBLE);

            sheetdetails.clear();

        }
    });

    lower.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

         upper_lay.setVisibility(View.GONE);
            upper.setTextColor(Color.parseColor("#ffffff"));
            lower.setTextColor(Color.parseColor("#F93249"));
           lower_lay.setVisibility(View.VISIBLE);

        }
    });

【问题讨论】:

  • 在哪里初始化 relative_layout_two ?
  • @IgorB 请查看我编辑的帖子。我初始化了这些布局
  • 我还是看不到。
  • 这个奇怪的事情发生了,看起来一切正常。你调试过监听器吗?是否调用了上层和下层点击侦听器?
  • 是的,他们正在正确调用。视图对我来说是不可见的。没有完全消失。当我触摸屏幕时,那些布局视图即将到来。重叠是问题

标签: android android-layout android-view


【解决方案1】:

尝试使用 View.GONE 不是 view.GONE

lower.setOnClickListener 不是lower.setOnTouchListener

【讨论】:

    【解决方案2】:

    View 是类,所以应该以大写字母开头。 请查看我的更新。

    upper.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            lower_lay.setVisibility(View.GONE);
            upper.setTextColor(Color.parseColor("#F93249"));
            lower.setTextColor(Color.parseColor("#ffffff"));
            upper_lay.setVisibility(View.VISIBLE);
    
        }
    });
    
    lower.setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            lower_lay.setVisibility(View.VISIBLE);
            upper.setTextColor(Color.parseColor("#ffffff"));
            lower.setTextColor(Color.parseColor("#F93249"));
            upper_lay.setVisibility(View.GONE);
        }
    });
    

    【讨论】:

      【解决方案3】:

      我会给你一个关于你的布局的好建议:

      来自 Chat Haase 博客:

      RelativeLayout 是一种使用起来非常方便的布局,因为它允许 开发人员指定内容应如何相对于其他内容放置 内容。在许多情况下,这是必要的,并且可能是最好的 工作的解决方案。但是,重要的是要了解 RelativeLayout 是一个昂贵的解决方案,因为它需要两个 测量通过以确保它已处理所有布局 正确的关系。此外,这个问题与每个 整个层次结构中的附加RelativeLayout。想象一个 位于视图层次结构顶部的 RelativeLayout;这本质上 将整个视图层次结构的测量工作加倍。现在 想象另一个 RelativeLayout 作为第一个的孩子之一 one — 这再次将其下发生的测量通过次数加倍, 需要对其中的所有视图进行四次测量 子层次结构。

      在不需要的情况下使用不同类型的布局 RelativeLayout 的功能,例如 LinearLayout 甚至自定义 布局。或者对于子视图相对对齐的情况 是必要的,考虑更优化的 GridLayout,它 预处理子视图关系并避免 双重测量问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-31
        • 2013-04-17
        • 2016-08-31
        • 1970-01-01
        • 1970-01-01
        • 2016-03-31
        相关资源
        最近更新 更多