标签: android-custom-view

自定义视图...覆盖onTouchEvent但不执行performClick

我在开发的自定义Android视图中收到此警告(来自问题标题).

为什么我会收到警告?它背后的逻辑是什么,即为什么在
覆盖performClick时也会覆盖它onTouchEvent

java android android-custom-view android-view

47
推荐指数
2
解决办法
3万
查看次数

Android Studio布局编辑器无法呈现自定义视图

在Android Studio中,布局编辑器无法在xml中预览自定义视图.

很简单的例子:

public class MyCustomView extends FrameLayout {
    public MyCustomView(Context context) {
        super(context);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
}
Run Code Online (Sandbox Code Playgroud)

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <com.myprojectxxx.view.MyCustomView
        android:layout_width="48dp"
        android:layout_height="48dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Android Studio总是说,

渲染问题

找不到以下类:

  • com.myprojectxxx.view.MyCustomView(修复构建路径,创建类)

提示:尝试构建项目

当然,我有那门课.如果我单击"创建类",它会抱怨同一个类已经存在.如果我重建该项目,没有任何改变.

而且,是的,该项目在我的Android设备上运行良好.此外,它在Eclipse ADT中呈现得非常好.但是,在Android Studio中,它总是说"CLASSES可能无法找到".

Android Studio无法使用自定义视图预览xml文件?这有什么问题?

java android android-custom-view android-layout android-studio

46
推荐指数
5
解决办法
2万
查看次数

MonoDroid:调用自定义视图的构造函数时出错 - TwoDScrollView

我正在构建一个Android应用程序,它使用在此处找到的自定义构建的TwoDScrollView:

http://blog.gorges.us/2010/06/android-two-dimensional-scrollview/

可以在其他几个网站上找到同一个类,Stack Overflow上的其他人也提出了有关它的问题.我在以前使用Java/Eclipse构建的Android应用程序中使用它,并且我取得了成功.

在我目前的应用程序中,我想使用C#和MonoDroid.我决定用C#重写整个TwoDScrollView类.在重写它,然后在一些布局XML中使用它之后,我在尝试运行我的代码时遇到以下异常:

抛出了System.NotSupportedException.无法从本机句柄44f4d310激活MyProject.TwoDScrollView类型的实例.

System.Exception:找不到MyProject.TwoDScrollView ::.ctor(System.IntPtr,Android.Runtime.JniHandleOwnership)......的构造函数......后面有更多文本....

我的布局XML如下:

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

<myproject.TwoDScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

</myproject.TwoDScrollView>

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

根据以下链接中有关在MonoDroid中使用布局XML中的自定义视图的说明:http://docs.xamarin.com/android/advanced_topics/using_custom_views_in_a_layout

TwoDScrollView类的构造函数如下所示:

public TwoDScrollView(Context context) 
    : base(context)
{
    initTwoDScrollView();
}

public TwoDScrollView(Context context, IAttributeSet attrs) 
    : base(context, attrs)
{
    initTwoDScrollView();
}

public TwoDScrollView(Context context, IAttributeSet attrs, int defStyle) 
    : base(context, attrs, defStyle)
{
    initTwoDScrollView();
}
Run Code Online (Sandbox Code Playgroud)

C#版本中存在与Java版本相同的构造函数(可以在上面的链接中找到).什么可能出错?如果有人想看,我可以发布我的TwoDScrollView的完整C#代码.除了用C#重写之外,它基本上与位的Java代码位相同.

谢谢你的帮助!

android scrollview android-custom-view android-layout xamarin.android

45
推荐指数
3
解决办法
3万
查看次数

在Android视图剪辑边界外绘图时:如何防止底层视图在我的自定义视图上绘制?

我写了一个自定义Android视图,需要在其剪切边界之外绘制.

这就是我所拥有的:

在此输入图像描述

这是当我点击一个按钮时发生的情况,例如右键:

在此输入图像描述

如何防止下面的视图在我的"句柄"上绘制?

我项目中的一些相关伪代码如下.

我的自定义视图MyHandleView如下所示:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Path p = mPath;
    int handleWidth = mHandleWidth;
    int handleHeight = mHandleHeight;
    int left = (getWidth() >> 1) - handleWidth;

    canvas.save();

    // allow drawing out of bounds vertically
    Rect clipBounds = canvas.getClipBounds();
    clipBounds.inset(0, -handleHeight);
    canvas.clipRect(clipBounds, Region.Op.REPLACE);

    // translate up to draw the handle
    canvas.translate(left, -handleHeight);

    // draw my background shape
    canvas.drawPath(p, mPaint);

    canvas.restore();
}
Run Code Online (Sandbox Code Playgroud)

布局是这样的(我简化了一点):

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- Main content of the SlidingUpPanel --> …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout

43
推荐指数
1
解决办法
5万
查看次数

如何在自定义视图中获取"android:"标记值

似乎有很多"类似"的问题和答案分散在其中,所有问题都涉及如何从中获取自定义属性AttributeSet.到目前为止我无法找到的是如何获取android:命名空间标记:

    <com.custom.view.StatusThumbnail
        android:id="@+id/statusThumbnailContainer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:layout_weight="1"/>
Run Code Online (Sandbox Code Playgroud)

我想layout_height从这个自定义组件中拉回属性.不幸的是,从我所读到的最近,我必须要做的是:

public StatusThumbnail(Context context, AttributeSet attrs) {
    super(context, attrs);

    String height = attrs.getAttributeValue("android", "layout_height");
Run Code Online (Sandbox Code Playgroud)

但这会回来null.

当然这不是一件罕见的事情吗?

android android-custom-view android-layout

42
推荐指数
1
解决办法
2万
查看次数

使用枚举作为自定义XML属性

我想在我的项目中使用自定义组件,我想将它添加到下面的枚举属性,我该怎么做?

<com.abb.abbcustomcompanents.buttons.AbbButton
        android:id="@+id/abbBtn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        app:Type="How can i use enum here"
        />

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="abbButton">
        <attr name="Type" format="enum"/>
        <attr name="onAction" format="string"/>
    </declare-styleable>
</resources>
Run Code Online (Sandbox Code Playgroud)

谢谢 !

android android-custom-view android-layout

42
推荐指数
1
解决办法
2万
查看次数

Android自定义形状按钮

如何在Android中制作自定义形状的可点击视图或按钮?

当我点击时,我想避免触摸空白区域.

在此输入图像描述

请帮忙.谢谢.

android android-widget android-custom-view android-layout onclicklistener

41
推荐指数
2
解决办法
6万
查看次数

在自定义视图中设置textSize会产生巨大的文本

我在自定义视图的构造函数中调用以下内容:

private void style(Resources.Theme theme, AttributeSet attrs) {
    TypedArray a = theme.obtainStyledAttributes(
            attrs,
            R.styleable.StackedTextView,
            0, 0);

    try {
        DebugTool.assertTrue(holdr != null, "View holder has not been properly intialized.");
        String line1 = a.getString(R.styleable.StackedTextView_line1);
        setLine1(line1);
        String line2 = a.getString(R.styleable.StackedTextView_line2);
        setLine2(line2);

        line1Size = a.getDimension(R.styleable.StackedTextView_line1_textSize, 20);
        line2Size = a.getDimension(R.styleable.StackedTextView_line2_textSize, 20);
        if (line1Size > 0) {
            holdr.textLine1.setTextSize(line1Size);
        }
        if (line2Size > 0) {
            holdr.textLine2.setTextSize(line2Size);
        }

    } finally {
        a.recycle();
    }
}
Run Code Online (Sandbox Code Playgroud)

它应该为2个文本字段设置文本和文本大小.

除了文本内容的字符串格式之外,我在attr.xml中还有以下内容(工作正常).

    <attr name="line1_textSize" format="dimension" />
    <attr name="line2_textSize" format="dimension" />
Run Code Online (Sandbox Code Playgroud)

当我使用此视图并通过xml使用维度设置文本大小时,

        <com.me.app.view.component.StackedTextView
            android:id="@+id/overview_total_reviews"
            app:line1="40" …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-layout

38
推荐指数
1
解决办法
8896
查看次数

膨胀自定义按钮类时出错.NoSuchMethodException

我正在尝试创建一个包含两个文本字段的自定义按钮.当我从代码创建实例时,它可以工作.当我尝试从XML充气时,它会崩溃.

到目前为止我尝试解决这个问题:

  • 遵循并理解SDK中的LabelView代码示例.
  • 接下来,了解这个优秀的Devoxx自定义FlowLayout演示文稿.
  • 跟随并理解了关于该主题的大量教程.
  • 花了不少时间审查stackoverflow上的类似问题.
  • 将代码剥离回简单地将属性集传递给超类的基础知识.

从我读过的问题来看,最常见的原因是没有定义获取属性集所需的构造函数即

MyClass(Context context, AttributeSet attrs) {
    super(context, attrs);
}
Run Code Online (Sandbox Code Playgroud)

在LogCat的错误列表的末尾附近,我有:

Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
Run Code Online (Sandbox Code Playgroud)

这可能会导致我得出与其他许多人一样的错误的结论,但是当你查看下面的代码时,你可以看到我已经定义了这个表单的构造函数.

我似乎完全像所有消息来源所说的那样做.我只是看不到我错过的东西.

SRC/com.soundconception.custombuttontest2/TitledValueButton.java

package com.soundconception.custombuttontest2;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.Button;

public class TitledValueButton extends Button {

    public TitledValueButton(Context context) {
        super(context);
    }

    protected TitledValueButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}
Run Code Online (Sandbox Code Playgroud)

RES /值/ attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="TitledValueButton">
        <attr name="titleText" format="string" />
    </declare-styleable>

</resources>
Run Code Online (Sandbox Code Playgroud)

RES /布局/ activity_main.xml中 …

android android-custom-view

36
推荐指数
3
解决办法
2万
查看次数

自定义视图如何访问其活动?

我有一个自定义视图,并希望访问其活动中可用的字符串.我见过getContext()在视图类中使用的代码,但没有方法可以访问通过intent为其活动提供的String.如何使活动中的String可用于其自定义视图?

android android-custom-view android-intent

31
推荐指数
1
解决办法
2万
查看次数