将布局与父级底部对齐不起作用 - 以编程方式设置布局高度后

时间:2015-01-19 05:27:28

标签: java android android-fragments

我正在尝试将片段放在屏幕的底部。通过设置属性android:layout_alignParentBottom="true",片段将作为我的要求。

现在我正在尝试以编程方式指定片段的高度。高度设置正确。但是片段没有放在父母的底部。

这是我的代码:

fragment_main.xml:

<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="200dp"
    android:layout_gravity="bottom"
    android:layout_alignParentBottom="true"
    android:layout_marginTop="30dp"
    android:background="#4e534d"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.gokul.myprogram.MainActivity$PlaceholderFragment" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

MainActivity.java:

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(screenWidth, screenHeight/2);  // Commented
        layoutParams.gravity = Gravity.BOTTOM;       // Commented
        rootView.setLayoutParams(layoutParams);      // Commented
        return rootView;
    }
}

没有评论的屏幕截图:

Without Commenting

屏幕截图评论:(请参阅// Commented

With Commenting

0 个答案:

没有答案