Custom view draws over other views

android android-layout

79 просмотра

1 ответ

I'm new to Android development and figure I've missed something small and crucial (either than or I'm doing something completely and utterly wrong). I've create a screen layout that adds a number of controls (toolbar, a custom view, and two buttons). All the custom view does at present is draws text on the screen. But, it's doing something I would not expect.

First of all, the text draws underneath the toolbar. This is not too big a deal as I can offset the drawing so that it appears on screen, but as my toolbar and view is in a vertically oriented Linear Layout, I would have expected my custom view to start immediately below the toolbar.

Anyway, my main problem is that when my custom view is added to the layout it seems to draw over the entire screen so that I cannot see the two buttons (if I remove my custom view the buttons are there).

Here is the layout of the activity:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/main_activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context="com.myapp.MainScreen">

    <android.support.v7.widget.Toolbar
        android:id="@+id/main_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>


    <com.myapp.MyControl
        android:id="@+id/my_control"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/main_buttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button android:id = "@+id/one"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button android:id = "@+id/two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>

</LinearLayout>

My control is very simple at the moment:

public class MyControl extends View {

    private String mText;

    private Paint mTextPaint;

    public MyControl(Context context, AttributeSet attributeSet){
        super(context, attributeSet);

        mText = "My Control";

        mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mTextPaint.setTextSize(100);
    }

    protected void onDraw(Canvas canvas)
    {
        canvas.drawText(mText, 5, 50, mTextPaint);
    }
}

Any help would be appreciated

Автор: Lord Byron Источник Размещён: 08.11.2019 11:05

Ответы (1)


0 плюса

Решение

Use this

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:id="@+id/main_activity"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.75">

            <com.thisana.RageMaker.Helpers.MyControl
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/view"
                android:layout_gravity="center_horizontal"/>
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.25"
            android:weightSum="1"
            android:gravity="center"
            >

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button"/>

            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New Button"
                android:id="@+id/button2"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

enter image description here

Автор: Sabish.M Размещён: 20.08.2016 09:03
Вопросы из категории :
32x32