java - android隐藏内部RelativeLayout防止 Activity 出现

标签 java android xml android-relativelayout

我有一个带有内部RelativeLayouts的RelativeLayout,

我需要根据某些逻辑隐藏其中的一些RelativeLayouts,

问题是,如果我隐藏任何内部的RelativeLayouts, Activity 就不会出现,

我有checked this page做我正在做的事情,但我的不起作用??

这是我的代码,

Activity 的 XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/warning_bgnd"
    android:orientation="vertical" >

<Button
    android:id="@+id/button_warning"
    style="@style/okWarningButton"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="62dp" />

<RelativeLayout
    android:id="@+id/layout_warningA"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="70dp">

    <ImageView
        android:id="@+id/imageViewA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_warning"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/warning_red"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/warningTitleA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="70dip"
        android:paddingTop="0px"
        android:text="@string/textarea_warning_title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/warningMessageA"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/warningTitleA"
        android:layout_marginTop="4dp"
        android:paddingLeft="70dip"
        android:text="@string/textarea_warning_cat_a"
        android:textColor="#B5B5B5"
        android:textSize="12sp" />
</RelativeLayout>


  <RelativeLayout
    android:id="@+id/layout_warningB"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="170dp">

    <ImageView
        android:id="@+id/imageViewB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_warning"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/warning_orange"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/warningTitleB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="70dip"
        android:paddingTop="0px"
        android:text="@string/textarea_warning_title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/warningMessageB"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/warningTitleB"
        android:layout_marginTop="4dp"
        android:paddingLeft="70dip"
        android:text="@string/textarea_warning_cat_b"
        android:textColor="#B5B5B5"
        android:textSize="12sp" />
</RelativeLayout>

  <RelativeLayout
    android:id="@+id/layout_warningC"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="275dp">

    <ImageView
        android:id="@+id/imageViewC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button_warning"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="18dp"
        android:layout_marginTop="10dp"
        android:src="@drawable/warning_green"
        tools:ignore="ContentDescription" />

    <TextView
        android:id="@+id/warningTitleC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="70dip"
        android:paddingTop="0px"
        android:text="@string/textarea_warning_title"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/warningMessageC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/warningTitleC"
        android:layout_marginTop="4dp"
        android:paddingLeft="70dip"
        android:text="@string/textarea_warning_cat_c"
        android:textColor="#B5B5B5"
        android:textSize="12sp" />
    </RelativeLayout>

 </RelativeLayout>

以及java文件中的代码

   public class WarningActivity extends PrestartAbstractActivity {

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_warning);

    //myWarningLayouts
    RelativeLayout layoutA = (RelativeLayout)this.findViewById(R.id.layout_warningA);
    RelativeLayout layoutB = (RelativeLayout)this.findViewById(R.id.layout_warningB);
    RelativeLayout layoutC = (RelativeLayout)this.findViewById(R.id.layout_warningC);




    //retrieve data for intent
    Intent intent = getIntent();
    String warningType = intent.getStringExtra("type");


    Log.d("mensa", "me entro en warning ::"+warningType);


    //TODO, IMAGE BUTTON!!

    Button newPreStartButton = (Button) findViewById(R.id.button_warning);
    newPreStartButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            restartForms();
        }
    }) ;


    //manage warning type

    int warningTypeInt = Integer.valueOf(warningType.toString());


    switch (warningTypeInt) {
    case 0:
        Log.d("mensa", "never happens");
        break;
    case 1:
        Log.d("mensa", "case 1");

        layoutA.setVisibility(View.VISIBLE);
        layoutB.setVisibility(View.INVISIBLE);
        layoutC.setVisibility(View.INVISIBLE);

        break;
    case 2:
        Log.d("mensa", "case 2");
        break;
    case 3:
        Log.d("mensa", "case 3");
        break;
    case 4:
        Log.d("mensa", "case 4");
        break;
    case 5:
        Log.d("mensa", "case 5");
        break;
    case 6:
        Log.d("mensa", "case 6");
        break;
    case 7:
        Log.d("mensa", "case 7");

        break;
    default:
    Log.d("mensa", "default switch");
    }

}

所以,在我的测试中,如果我有情况 1, 永远不会被调用,甚至看不到 Activity 的内部日志?

隐藏我的内部布局缺少什么?

谢谢!

最佳答案

您尚未定义按钮的layout_width 和layout_height。检查 logcat 应该会给出某种错误。当您为按钮提供宽度和高度并提供一些要在按钮上显示的文本或图像时,此代码将起作用。您可以通过以下方式做到这一点

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warningBtnText"

关于java - android隐藏内部RelativeLayout防止 Activity 出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16432361/

相关文章:

.net - 将简单字符串参数传递给 .net webservice 时出现问题

python 以一种特定模式解析 xml

java - 是否可以在 Java 中按值传递数组(而不是引用)?

java jsonxpth 解析json跳转到节点

android - 如何在 android 停止后再次播放歌曲?

android - 如何在expo react native App中将api级别29更改为30

java - Lex Bot 与 Rest Api 集成

java - 如何判断 Java import 语句何时使用通配符匹配?

python - 如何使菜单对于 openerp 7 中特定组下的用户不可见?

python - 使用 ElementTree 在 Python 中解析 XML