【问题标题】:Custom view draws over other views自定义视图绘制在其他视图之上
【发布时间】:2016-12-27 09:34:00
【问题描述】:

我是 Android 开发的新手,我认为我错过了一些小而重要的事情(或者我正在做一些完全错误的事情)。 我创建了一个添加了许多控件(工具栏、自定义视图和两个按钮)的屏幕布局。 目前自定义视图所做的只是在屏幕上绘制文本。但是,它正在做一些我没想到的事情。

首先,文本在工具栏下方绘制。这没什么大不了的,因为我可以偏移绘图以使其显示在屏幕上,但由于我的工具栏和视图处于垂直方向的线性布局中,我希望我的自定义视图立即在工具栏下方开始。

无论如何,我的主要问题是,当我的自定义视图添加到布局时,它似乎会在整个屏幕上绘制,因此我看不到这两个按钮(如果我删除我的自定义视图,按钮就在那里)。

这是活动的布局:

<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>

此刻我的控制很简单:

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);
    }
}

任何帮助将不胜感激

【问题讨论】:

  • 使用相对布局而不是线性布局。

标签: android android-layout


【解决方案1】:

使用这个

    <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>

【讨论】:

  • 这似乎不太奏效。是的,我现在可以看到我的按钮,但我的视图没有绘制到屏幕上。请注意,我在最后添加了上面缺少的 。
  • 无变化。我视图中的文本仍然没有绘制到我的屏幕上。确实调用了 onDraw 方法,但没有绘制任何内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多