如何在屏幕上显示包含按钮的布局

时间:2015-03-16 11:40:29

标签: android

通过下面的布局,按钮在页面顶部显示 。如何在容器后面显示它们在页面上

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:id="@+id/container"
        xmlns:android="http://schemas.android.com/apk/res/android">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_gravity="center_vertical|center_horizontal">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/Main"
            android:onClick="ClickHomePage"
            android:text= "@string/Home"
            />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/map"
            android:onClick="ClickClients"
            android:text= "@string/Clients"
            >

        </Button>


        </LinearLayout>
    </LinearLayout>

MainActivity如下所示,HomePage.xml也在

之下
 public class MainActivity extends ActionBarActivity
             {



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            GetButtonClicked(5);
            setContentView(R.layout.activity_main);
        }



        public void GetButtonClicked(int position)
        {
            // update the main content by replacing fragments

            Fragment fragment = null;
            switch (position){ 
                case 0:
                    fragment= new FirstPageFragment();
                    break;

                default:
                    fragment =  new FirstPageFragment();
                    break;
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();
        }

        public  void ClickHomePage(View view){
            GetButtonClicked(0);
        }
        public  void ClickClients(View view){
            GetButtonClicked();
        }

        }




   <FrameLayout 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" tools:context="com.test.FirstPageFragment" android:background="@drawable/theme">
</FrameLayout>

这是第一页

public class FirstPageFragment extends Fragment {


public FirstPageFragment() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_first_page, container, false);
}

}

5 个答案:

答案 0 :(得分:0)

使用relativelayout而不是线性。

答案 1 :(得分:0)

为底部按钮设置不同的布局,然后在容器布局的最后一个使用:

<include android:id="@+id/bottomLayout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    layout="@layout/bottom_buttons"/>

答案 2 :(得分:0)

enter image description here 使用RelativeLayout和android:layout_alignParentBottom="true"分步指南here

您的布局骨架应如下所示

<RelativeLayout

           <LinearLayout android:layout_alignParentBottom="true">

              <Button
              <Button

           </LinearLayout> 

</RelativeLayout>

最终版面

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="ClickHomePage"
        android:text="Home" />

    <Button
        android:layout_marginLeft="30dp"
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="ClickClients"
        android:text="Clients" >
    </Button>
</LinearLayout>

答案 3 :(得分:0)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/Main"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="ClickHomePage"
        android:text="@string/Home" />

    <Button
        android:id="@+id/map"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:onClick="ClickClients"
        android:text="@string/Clients" >
    </Button>
</LinearLayout>

试试这个

答案 4 :(得分:0)

问题是我没有将容器包含在另一个布局中......

 <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"
    tools:context="com.test.MainPageActivityActivity">
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/container">
    </LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:layout_alignParentTop="@+id/container">
and insert buttons layout here....
 <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:id="@+id/Main"
            android:onClick="ClickHomePage"
            android:text= "@string/Home"
           />