Android xml布局文本可见性问题

时间:2015-05-25 10:17:49

标签: android android-layout

我正在使用android xml布局。所以,我需要两个像两个textview的截图。我的文本视图重叠,文本颜色错误。 Text1颜色不错,Text2颜色错误。我怎么能解决这个问题?

bringToFront(); and invalidate();

这对我没有帮助。请参阅下面的代码并提示我解决方案。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/exercise_background"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <com.ui.customviews.SquareLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/assign_circle"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/category2"
                android:text="test"
                android:layout_gravity="bottom"
                android:textSize="30sp"
                android:textColor="#C5CACF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </com.ui.customviews.SquareLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:visibility="invisible" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:visibility="invisible" />

        <com.ui.customviews.SquareLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/assign_circle"
            android:gravity="center_horizontal"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/category1"
                android:text="test"
                android:textSize="30sp"
                android:textColor="#C5CACF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </com.ui.customviews.SquareLayout>

    </LinearLayout>
</RelativeLayout>

enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

我认为主要问题是您的第一个文本视图位于第二个圆形图像后面。

而不是在第一个线性布局中提及第二个布局。并使其重心,以便您可以将其排列在第二个圆形图像上,它不会重叠。

答案 1 :(得分:0)

在你的xml文件中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/exercise_background"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <com.ui.customviews.SquareLayout
            android:id="@+id/square_1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/assign_circle"
            android:gravity="center_horizontal"
            android:orientation="horizontal" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:visibility="invisible" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:orientation="horizontal"
            android:visibility="invisible" />

        <com.ui.customviews.SquareLayout
            android:id="@+id/square_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/assign_circle"
            android:gravity="center_horizontal"
            android:orientation="horizontal" />

    </LinearLayout>

    <TextView
                android:id="@+id/category2"
                android:text="test1"
                android:layout_centerHorizontal="true"
                android:layout_gravity="bottom"
                android:textSize="30sp"
                android:textColor="#C5CACF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

    <TextView
                android:id="@+id/category1"
                android:text="test2"
                android:layout_centerHorizontal="true"
                android:textSize="30sp"
                android:textColor="#C5CACF"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

</RelativeLayout>

在java代码中:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        SquareLayout layout1 = (SquareLayout) findViewById(R.id.square_1);
        SquareLayout layout2 = (SquareLayout) findViewById(R.id.square_2);
        TextView categoryTop = (TextView) findViewById(R.id.category2);
        TextView categoryBottom = (TextView) findViewById(R.id.category1);

        int marginForTopCategory = dpFromPx(this, Integer.valueOf(layout2.getBottom() + categoryTop.getHeight() * 2).floatValue());
        int marginForBottomCategory = Float.valueOf(dpFromPx(this, Integer.valueOf(layout1.getBottom() - categoryBottom.getHeight() / 2))).intValue();

        ViewGroup.MarginLayoutParams lp = (ViewGroup.MarginLayoutParams)categoryTop.getLayoutParams();
        lp.topMargin = marginForTopCategory;
        categoryTop.setLayoutParams(lp);

        lp = (ViewGroup.MarginLayoutParams)categoryBottom.getLayoutParams();
        lp.setMargins(0, marginForBottomCategory, 0, 0);
        categoryBottom.setLayoutParams(lp);
    }

    public static int dpFromPx(final Context context, final float px) {
        float result = px / context.getResources().getDisplayMetrics().density;
        return Math.round(result);
    }