Android square LinearLayout没有显示内容

时间:2016-08-05 23:28:28

标签: android android-layout

我需要在我的应用上显示一个方形的LinearLayout,并且我创建了一个扩展LinearLayout的自定义类,因此它是正方形的:

public class SquareLinearLayout extends LinearLayout {

    public SquareLinearLayout(Context context) {
        super(context);
    }

    public SquareLinearLayout(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareLinearLayout(final Context context, final AttributeSet attrs, final int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
        setMeasuredDimension(height, height);
    }

    @Override
    protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
        super.onSizeChanged(w, w, oldw, oldh);
    }
}

然后,在布局上我添加线性布局并在其中放置一个按钮,例如,问题是虽然布局工作并显示(如果您将其背景设置为任何颜色,它是可见的),元素布局内部没有显示,好像它是空的:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.javierd.iifym.MainActivity">

    <com.javierd.iifym.SquareLinearLayout
        android:id="@+id/CaloriesLayout"
        android:layout_above="@+id/macronutrientsTable"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_gravity="center_horizontal|top"
        android:background="#ff0000">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:text="Test"
            android:id="@+id/textView"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true" />
    </com.javierd.iifym.SquareLinearLayout>

    <RelativeLayout
        android:id="@+id/macronutrientsTable"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="@dimen/fab_margin">

        <!-- IRRELEVANT CONTENT THAT FILLS THE SCREEN -->

    </RelativeLayout>
</RelativeLayout>

enter image description here

有人可以帮我解决这个问题吗?非常感谢!

0 个答案:

没有答案