发布定位自定义LinearLayout

时间:2014-10-05 14:13:18

标签: android android-layout

我创建了一个自定义线性布局,以使“方形”布局适合其父级的宽度,但我无法将其与另一个LinearLayout定位。 我希望LinearLayout位于SquareLinearLayout之下,我得到了这个(两个布局都是顶部对齐的):

enter image description here

我已经尝试过android:parentAlignBottom="true"android:gravity="bottom",但对我来说没有任何作用......

这是我的布局xml:

<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"
    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.dekajoo.rpg.MainActivity" >

    <com.dekajoo.rpg.custom_layouts.SquareLinearLayout
        android:id="@+id/game_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:background="#f00" />

    <android:LinearLayout
        android:id="@+id/control_bar"
        android:layout_width="match_parent"
        android:layout_height="100dip"
        android:background="#0f0"
        android:gravity="center"
        android:orientation="horizontal" />

</RelativeLayout>

这是我的SquareLinearLayout类:

package com.dekajoo.rpg.custom_layouts;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class SquareLinearLayout extends LinearLayout {

    public SquareLinearLayout(Context context) {
        super(context);
    }
    public SquareLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /*public SquareView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }*/

    @Override 
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }
}

谢谢,

1 个答案:

答案 0 :(得分:0)

将此添加到您的LinearLayoutandroid:layout_below="@id/game_layout"