android 如何在布局下方设置按钮

How To Set Button Below In Layout in android

你好我想在底部设置两个按钮 我已经尝试了很多但无法设置我想要底部的左右按钮 这是我的 letsbounced

<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" >

<com.example.bounced.card.SwipeFlingAdapterView
    android:id="@+id/frame"
    android:background="#ffeee9e2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rotation_degrees="15.5"
    tools:context="com.example.bounced.SlidingMenuActivity" />

<include layout="@layout/buttons"

      />

这是按钮布局

    <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_gravity="bottom"

    android:layout_marginBottom="40dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:id="@+id/left"
        android:layout_margin="10dp"
        android:text="Left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/right"
        android:layout_margin="10dp"
        android:text="Right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这是我的片段

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    RelativeLayout wrapper = new RelativeLayout(getActivity());
    // for example
     inflater.inflate(R.layout.letsbounced, wrapper, true);


      Button left=(Button)wrapper.findViewById(R.id.left);
        Button right=(Button)wrapper.findViewById(R.id.right);
        left.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View wrapper) {
                // TODO Auto-generated method stub
                left();
            }
        });
 right.setOnClickListener(new OnClickListener() {

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

我正在发布布局的屏幕截图..我想在底部设置按钮 我试了很多

在线性布局中使用gravity

示例:

android:gravity="bottom";

为了在线性布局中对齐底部视图,只需添加一个空视图并设置 weight="1" 这样它将占据所有空视图 space。

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    android:layout_gravity="bottom"
                    android:orientation="vertical"
                    android:layout_marginBottom="40dp"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <View
                        android:layout_width="0dp"
                        android:layout_height="0dp"
                        android:layout_weight="1" />

                    <LinearLayout
                        android:orientation="horizontal"
                        android:layout_gravity="bottom"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content">

                        <Button
                            android:id="@+id/left"
                            android:layout_margin="10dp"
                            android:text="Left"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                        <Button
                            android:id="@+id/right"
                            android:layout_margin="10dp"
                            android:text="Right"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content" />

                    </LinearLayout>
                </LinearLayout>

或者更好,使用 RelativeLayout 并使用 alignParentBottom = "true"

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_gravity="bottom"
    android:orientation="vertical"
    android:layout_marginBottom="40dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

        <Button
            android:id="@+id/left"
            android:layout_margin="10dp"
            android:text="Left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/right"
            android:layout_margin="10dp"
            android:text="Right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</RelativeLayout>