我有一个内部RelativeLayouts的RelativeLayout,
我需要根据某些逻辑隐藏其中的一些RelativeLayouts,
问题是,如果我隐藏了我的任何内部RelativeLayouts,则会显示活动doesent,
我有checked this page做我正在做的事情,但我的工作不起作用?
这是我的代码,
活动的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, 永远不会被调用,甚至看不到活动的内部日志?
隐藏内部布局缺少什么?
谢谢!
答案 0 :(得分:1)
您尚未为按钮定义layout_width和layout_height。检查logcat它应该给出某种错误。当您为按钮提供宽度和高度并提供要在按钮上显示的某些文本或图像时,此代码有效。你可以通过
来做到这一点android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/warningBtnText"