Android snippet behaves strangely

Activity_main.xml is as follows

<LinearLayout 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" > <Button android:id="@+id/button_one_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Button" /> <fragment android:name="fragments.FirstFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/first_fragment" /> <Button android:id="@+id/button_two_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Second Button" /> </LinearLayout> 

The main activity class is similar to this

 package com.example.testfragmentshoneycomb; import android.os.Bundle; import android.app.Activity; import android.view.Menu; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } } 

first_fragment.xml is as follows

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:background="@color/grey" >" <TextView android:id="@+id/text_view_one_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View one" /> <TextView android:id="@+id/text_view_two_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View two" /> <TextView android:id="@+id/text_view_three_fragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Text View three" /> </LinearLayout> 

The FirstFragment class is similar to this

 package fragments; import com.example.testfragmentshoneycomb.R; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class FirstFragment extends Fragment{ @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { View view = inflater.inflate(R.layout.first_fragment, container, false); return view; } } 

Shows only the first button and nothing else on the screen. If I remove the first button from Activity_main.xml, it will display the fragment, but not the second button.

The minimum version of the SDK is 11, and the build goal is Android 4.1.

+4
source share
4 answers

set android:orientation="vertical" to your action layout.

+3
source

This is because the default orientation is LinearLayout horizontal . Therefore, the width of all screens is fixed using the First Button and Fragment .

Are you sure you want to see this?

 First_Button Fragment Second_Button 

If yes, use layout_weight . If No, give orientation=vertical LinearLayout, which will show your layout output as

 First_Button Fragment Second_Button 
+3
source

set the LinearLayout orientation to vertical, horizontal to horizontal. read the documents carefully

+1
source

Use the following layout:

  <LinearLayout 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:orientation="vertical" > <Button android:id="@+id/button_one_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="First Button" /> <fragment android:name="fragments.FirstFragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/first_fragment" /> <Button android:id="@+id/button_two_activity_one" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Second Button" /> </LinearLayout> 
+1
source

All Articles