【问题标题】:Draw View from XML file under other TextView在其他 TextView 下从 XML 文件中绘制视图
【发布时间】:2016-10-27 21:14:30
【问题描述】:

我正在尝试在视图中绘制画布并在我在 XML 文件中定义的一些 TextView 下显示此视图。 每次我测试应用程序时,它都不会在设备上启动。

但是,这些部分本身可以工作: 当我将onCreate() 中的setContentView(R.layout.activity_main); 更改为setContetView(new CustomView(this)); 时,它可以工作,但当然没有textView。

我的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainLayout"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/sampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"/>

    <com.example.simon.drawtest.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sampleText"
        android:layout_alignParentBottom="true"/>

</RelativeLayout>

我的MainActivity.java

package com.example.simon.drawtest;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
 }
}

我的CustomView.java

package com.example.simon.drawtest;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class CustomView extends View {

    private Paint paint;

    public CustomView(Context context) {
        super(context);   
        paint = new Paint();
        paint.setColor(Color.GRAY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawColor(Color.BLUE);
        canvas.drawCircle(200, 200, 100, paint);
    }
}

【问题讨论】:

  • 你不应该把你的自定义视图放在第一位吗?意味着在文件中更高,在 TextView 之前?
  • @milosmns 并没有什么不同。

标签: android xml canvas view


【解决方案1】:

展示你的整个activity_main.xml...

Views 都应该包含在例如FrameLayout

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/sampleText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some Text"/>
    <com.example.simon.drawtest.CustomView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/sampleText"
        android:layout_alignParentBottom="true"/>
</FrameLayout>

编辑:好的,所以你有 RelativeLayout.. 还请注意,在 Android 5.0 中引入了 elevation attribute,你可能会遇到一些绘图顺序问题,例如 HERE

另外...您的TextView 在 XML 中是第一个,因此它将在您的自定义视图之前绘制(如果未设置高度,则按顺序绘制)

也...android:layout_below="@+id/sampleText" 下方表示垂直轴下方。尝试删除此行

编辑:用户在 cmets 中发布异常堆栈跟踪,所以下面我为他的案例粘贴正确的答案(复制评论):

你的错误堆栈说

引起:java.lang.NoSuchMethodException: [类android.content.Context,接口 android.util.AttributeSet]

你只有一个带有 Context 的构造函数 只是,系统还需要一个带有附加属性集。退房 THIS 问答,为您的 CustomView 应用适当的构造函数

【讨论】:

  • 添加了其余部分。我把它们包裹在RelativeLayout中。有区别吗(对问题很重要)?
  • 调用super.onDraw() 只会绘制背景和任何已注册的drawable,对吗?它可能“只是”布局中子元素的顺序:)
  • 在这个简单的例子中在onDraw 中调用 super 肯定只会绘制透明背景(所以.. 什么都没有),但是当项目增长时,这个 super 调用可能会做一些额外的工作,尤其是当用户正在绘图时自定义形状或使用一些材料引入的参数/属性:)
  • 好的,我删除了 belowattribute 并将 CustomView 移到 TextView 之前。问题依然存在。我是否真的调用了 CustomView 的实例?
  • 尝试为您的Views 设置固定尺寸 - 例如layout_widthlayout_height 为 100dp。请注意,wrap_content 无法识别在 onDraw 内绘制的自定义形状,并且此 View 的大小将是 0px x 0px(您还应该覆盖 onMeasure 方法并在其中设置尺寸)。当您使用 setContetView(new CustomView(this)); 时它会起作用,因为然后您的 View fullfil 整个 Activity
猜你喜欢
  • 1970-01-01
  • 2019-11-21
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多