在FrameLayout

时间:2016-08-25 15:13:46

标签: android textview android-framelayout

我写了一个自定义的GroupView类,它在X by Y网格中显示它的子节点。孩子们是正方形。

package com.roundel.calc2;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CalcPadLayout extends ViewGroup {
private int mRowCount;
private int mColumnCount;
public CalcPadLayout(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    final TypedArray a = context.obtainStyledAttributes(attrs,
            new int[] { android.R.attr.rowCount, android.R.attr.columnCount }, defStyle, 0);
    mRowCount = a.getInt(0, 1);
    mColumnCount = a.getInt(1, 1);
    a.recycle();
}

public CalcPadLayout(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CalcPadLayout(Context context) {
    this(context, null);
}

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    final int paddingLeft = getPaddingLeft();
    final int paddingRight = getPaddingRight();
    final int paddingTop = getPaddingTop();
    final int paddingBottom = getPaddingBottom();

    final int childCount = getChildCount();
    final int height = bottom-top-paddingBottom-paddingTop;
    final int width = right-left-paddingLeft-paddingRight;
    final int childSize = Math.min(Math.round(width/mColumnCount), Math.round(height/mRowCount));

    /*DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    float dpHeight = displayMetrics.heightPixels / displayMetrics.density;
    float dpWidth = displayMetrics.widthPixels / displayMetrics.density;*/
    int rowIndex = 0, columnIndex = 0;
    for(int i = 0; i < childCount; i++)
    {
        View child = getChildAt(i);
        final int childTop = rowIndex*childSize+paddingTop;
        final int childLeft = columnIndex*childSize+paddingLeft;
        final int childBottom = childTop+childSize;
        final int childRight = childLeft+childSize;
        //child.setGravity(Gravity.CENTER_HORIZONTAL);
        child.layout(childLeft, childTop, childRight, childBottom);
        rowIndex = (rowIndex + (columnIndex + 1) / mColumnCount) % mRowCount;
        columnIndex = (columnIndex + 1) % mColumnCount;
    }
    LayoutParams params = this.getLayoutParams();
    params.height = childSize*mRowCount+paddingTop+paddingBottom;
    this.setLayoutParams(params);
}
}

这是我的main_layout.xml。让我们在两个FrameLayouts中显示两个TextViews,我将设置FrameLayouts的背景颜色,这样我就可以看到它们是否正常显示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="bottom">

    <com.roundel.calc2.CalcPadLayout
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:rowCount="1"
        android:columnCount="2"
        android:background="@color/greyDark"
        android:gravity="bottom">


        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="35sp"/>
        </FrameLayout>

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:background="@color/colorPrimaryDark">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/textView2"
                android:gravity="center"
                android:textColor="@color/white"
                android:textSize="35sp"/>
        </FrameLayout>

    </com.roundel.calc2.CalcPadLayout>
</LinearLayout>

布局正确显示,但TextView不可见 Inside FrameLayouts 当我删除FrameLayout时,它可以正常工作,但是无论如何,文本只是垂直居中而不是水平。 Outside the FrameLayouts

1 个答案:

答案 0 :(得分:0)

如果这是您的最终布局,则不需要FrameLayout。 您可以直接在textView中设置背景颜色。 尝试使用尽可能少的布局级别。

同样对于文本对齐,这是因为textView存根具有“Wrap_content”值。

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:gravity="bottom">
    <com.roundel.calc2.CalcPadLayout
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:rowCount="1"
        android:columnCount="2"
        android:background="@color/greyDark"
        android:gravity="bottom">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Text"
            android:id="@+id/textView"
            android:gravity="center"
            android:textColor="@color/white"
            android:background="@color/colorPrimary"
            android:textSize="35sp"/>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="New Text"
            android:id="@+id/textView2"
            android:gravity="center"
            android:background="@color/colorPrimaryDark"
            android:textColor="@color/white"
            android:textSize="35sp"/>
    </com.roundel.calc2.CalcPadLayout>
</LinearLayout>