开发者

Android layouts question

What am i doing wrong in the following. also following are my questions

1.In one main.xml file can there be two linear layouts or tale layouts

2.Below on click of button1 i set linearlayout1's visibility to 'Invisible' but it doesn't happen.

super.onCreate(savedInstanceState);
setContentView(R.layout.main);



Button g_gal =(Button)findViewById(R.id.button1);
final LinearLayout tr1 =(LinearLayout)findViewById(R.id.linearLayout1);
final LinearLayout tr2 =(LinearLayout)findViewById(R.id.linearLayout2);
tr2.setVisibility(View.INVISIBLE);

final Button g_gal =(Button)findViewById(R.id.button1);
g_gal.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {


        try {
                    tr2.setVisibility(View.VISIBLE);
                    tr1.setVisibility(View.INVISIBLE);


        }
        catch (Exception e) 
        {
            Toast.makeText(getApplicationContext(),"Exception while displaying gallery: "+ e, Toast.LENGTH_SHORT).show();
        }
    }
 });

main.xml

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="wrap_content" and开发者_如何学Croid:layout_height="wrap_content">
    <Button android:text="Browse Gallery" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

   </LinearLayout>
   <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
      <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content">
         <requestFocus></requestFocus>
      </EditText>
      <Button android:text="Send SMS to Nav" android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      <Button android:text="Back" android:id="@+id/back1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>


   </LinearLayout>


A valid XML file can only have one root, so you can't have two linear layouts as root elements. Create another layout that contains those two layouts as children.


main.xml

  <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical" android:id="@+id/linearLayout0" android:layout_width="wrap_content" android:layout_height="wrap_content">
   <LinearLayout android:orientation="vertical" android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <Button android:text="Browse Gallery" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>

   </LinearLayout>
   <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
      <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content">
         <requestFocus></requestFocus>
      </EditText>
      <Button android:text="Send SMS to Nav" android:id="@+id/sms" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
      <Button android:text="Back" android:id="@+id/back1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
   </LinearLayout> 


1.add all other layout under the first LinearLayout.Now you are just finishing the LinearLayout after adding a LinearLayout and a Button

2.To hide layout tr1 try to hide the child

int count=tr1.getChildCount();
         for(int i=0;i<count;i++)
             tr1.getChildAt(i).setVisible(false);


For question 2. Instead of setting a View to invisible, set the visibility to View.Gone

final LinearLayout tr1 =(LinearLayout)findViewById(R.id.linearLayout1);
tr1.setVisibility(View.Gone)

This is better for layout purposes. Refer to the Android View doc for more details.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜