标签: android-custom-view

View.onTouchEvent仅注册ACTION_DOWN事件

我正在实现一个自定义TextView,我想在触摸视图时执行一些操作.我认为该onTouchEvent方法可以在不必使用的情况下为视图提供全方位的触摸setOnTouchListener(我正在尝试在视图中而不是在活动中完成所有工作,因此它是可移植的),但是唯一注册的触摸事件是ACTION_DOWN.如果我OnTouchListener使用此设置活动,我会获得全部触摸事件,但onTouchEvent不会.

任何人都知道为什么会这样,或者任何人都可以提供一个不涉及使用的解决方案setOnTouchListener(这会阻止实现活动设置自己的监听器)?

android android-custom-view touch-event android-event

24
推荐指数
1
解决办法
8965
查看次数

自定义attr获取颜色返回无效值

我有一个自定义视图,我想在其中设置textview的颜色.

我有

attrs.xml

<declare-styleable name="PropertyView">
    <attr name="propertyTitle" format="string" localization="suggested" />
    <attr name="showTitle" format="boolean" />
    <attr name="propertyTextColor" format="color" />
    <attr name="propertyTextSize" format="dimension" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

我在布局文件中设置它

<com.something.views.PropertyView
    android:id="@+id/dwf_rAwayTeamTimePenaltyInput"
    style="@style/mw"
    propertyview:propertyTextSize="16sp"
    propertyview:propertyTitle="@string/AwayTeam"
    propertyview:showTitle="true"
    propertyview:propertyTextColor="@color/textLight" />
Run Code Online (Sandbox Code Playgroud)

在我的代码中我设置它

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PropertyView, 0, 0);

    showTitle = a.getBoolean(R.styleable.PropertyView_showTitle, false);
    String title = a.getString(R.styleable.PropertyView_propertyTitle);
    float textSize = a.getDimension(R.styleable.PropertyView_propertyTextSize, -1);
    int color = a.getColor(R.styleable.PropertyView_propertyTextColor, -1);
    textSize = textSize / getResources().getDisplayMetrics().scaledDensity;
    if(BuildConfig.DEBUG) Log.e(getClass().getName(), "Color set to: " + color);

    setShowTitle(showTitle);
    setTitle(title);
    if(textSize >= 0) mTitleTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
    if(color != …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view

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

自定义Android calendarView

我正在寻找一个可以在这样的对话框中使用的CalendarView:

我计划使用android.widget.CalendarView但它可以从API级别11获得.考虑到仍有很多用户使用android 2.3.3(但不是主要的用户可以放弃那些用户)这是一个问题.

我应该使用以下哪个选项?

1)可用的库并相应地使用它们.

2)定制android.widget.CalendarView

如果是#2,那么它有什么例子吗?

android android-custom-view calendarview

24
推荐指数
1
解决办法
7万
查看次数

Android - 在运行时更改自定义标题视图

我在我的应用程序中为每个活动使用自定义标题视图.在其中一个活动中,基于按钮点击,我需要更改自定义标题视图.现在,每当我调用setFeatureInt时,这都可以正常工作.

但是,如果我尝试更新自定义标题中的任何项目(例如更改按钮的文本或标题上的文本视图),则不会进行更新.

通过代码调试显示文本视图和按钮实例不为空,我还可以看到自定义标题栏.但文本视图或按钮上的文本未更新.还有其他人遇到过这个问题吗?我该如何解决?

谢谢.

编辑

这是我尝试过的.即使在调用postInvalidate时也不会更新.

    getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.text_title);

    TextView databar = (TextView) findViewById(R.id.title_text);
    databar.setText("Some Text");
    databar.postInvalidate();

    Button leftButton = (Button) findViewById(R.id.left_btn);
    leftButton.setOnClickListener(mLeftListener);
    leftButton.setText("Left Btn");
    leftButton.postInvalidate();

    Button rightBtn = (Button) findViewById(R.id.right_btn);
    rightBtn.setOnClickListener(mRightListener);
    rightBtn.postInvalidate();
Run Code Online (Sandbox Code Playgroud)

android android-custom-view

23
推荐指数
2
解决办法
4万
查看次数

Android:如何测试自定义视图?

在Android中有几种单元测试方法,测试我编写的自定义视图的最佳方法是什么?

我目前正在测试它作为我在仪器测试案例中的活动的一部分,但我宁愿只测试视图,孤立.

android unit-testing android-custom-view android-view

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

在Android 5.0.2上不调用自定义视图构造函数

我创建了一个自定义视图:

public class SomeView extends View
Run Code Online (Sandbox Code Playgroud)

自定义视图构造函数:

public SomeView (Context context)
{
    super(context);
}
// Called when view is inflated from xml
public SomeView (Context context, AttributeSet attrs)
{
    super(context, attrs);
}
// Perform inflation from XML and apply a class-specific base style from a theme attribute.
public SomeView (Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
}
Run Code Online (Sandbox Code Playgroud)

我也尝试了api 21中的第4个构造函数而没有运气:

public VeediView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
    super(context, attrs,defStyleAttr, defStyleRes);
}
Run Code Online (Sandbox Code Playgroud)

在xml布局中,我定义了这个视图,并且工作正常.

在Galaxy S2上进行测试工作正常并调用视图构造函数,但是当在Nexus-7 android …

android android-custom-view

22
推荐指数
3
解决办法
1970
查看次数

RecyclerView和数据绑定无法正常工作

这是我第一次使用RecyclerView进行数据绑定,但不是我第一次使用RecyclerView本身.

由于某些原因,没有调用任何适配器方法 - 甚至不是getItemCount().我的RecyclerView可能是一个愚蠢的问题,与数据绑定无关,但我看不出有什么不妥.

       View rootview = inflater.inflate(R.layout.fragment_profile_first, container, false);
    // Initialize recycler view

    RecyclerView badgesRV = (RecyclerView) rootview.findViewById(R.id.badgesRV);
    LinearLayoutManager llm = new LinearLayoutManager(getActivity());
    llm.setOrientation(LinearLayoutManager.HORIZONTAL);
    badgesRV.setLayoutManager(llm);

    BadgeAdapter badgeAdapter = new BadgeAdapter(profileObject.badgesEntity.badges);
    badgesRV.setAdapter(badgeAdapter);
Run Code Online (Sandbox Code Playgroud)

适配器:

    public class BadgeAdapter extends RecyclerView.Adapter<BadgeAdapter.BadgeBindingHolder>{

    private static final int MAX_BADGES_TO_DISPLAY = 5;
    private BadgeObject[] badges;

    public BadgeAdapter(BadgeObject[] badges){
        this.badges = badges;
    }

    @Override
    public BadgeBindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.profile_badge_row, parent, false);
        BadgeBindingHolder holder = new BadgeBindingHolder(v);
        return holder;
    }

    @Override
    public …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-recyclerview android-databinding

22
推荐指数
1
解决办法
8456
查看次数

StaticLayout如何在Android中使用?

我需要构建自己的自定义,TextView所以我一直在学习StaticLayout在画布上绘制文本.这比Canvas.drawText()直接使用更好,或者说文档说的.但是,文档没有给出任何示例.只有模糊的参考才能StaticLayout.Builder成为更新的方法.

我在这里找到了一个例子,但似乎有点过时了.

我终于工作了,但是我在下面添加了我的解释.

android text canvas android-custom-view staticlayout

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

自定义EditText未在焦点上显示键盘

我正在创建一个自定义的EditText类,因为我需要设置一些自定义字体; 但是现在当我点击editText时,android键盘不再弹出...

这是我的班级:

    package ro.gebs.captoom.utils.fonts;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import ro.gebs.captoom.R;

public class CustomFontEditText extends EditText {

    private Context context;

    public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (!isInEditMode()) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomFontEditText,
                    defStyle, 0);

            assert a != null;
            int fontId = a.getInteger(R.styleable.CustomFontEditText_fontNameEdit, -1);
            if (fontId == -1) {
                throw new IllegalArgumentException("The font_name attribute is required and must refer "
                        + "to a valid …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view android-input-method android-edittext

20
推荐指数
1
解决办法
8191
查看次数

查看外部边界未正确绘制

点击自定义条形图(使用MPAndroidChart创建)后,我正在绘制工具提示.视图层次结构如下

<LinearLayout>
     <TextView text=Move & Max Pain/>
     <RelativeLayout with 2 textviews>
     <chart
       clipToChildren=false
       clipToPadding=false
     />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

虽然View位于Chart或其中间兄弟中,但外观看起来很好.但是当它与它的兄弟碰撞时,工具提示被截断了

在此输入图像描述

使用HierarchyViewer我可以看到内容存在,但它没有被绘制.

为了获得剪辑,我在draw中使用了这个代码

    @Override
  public void draw(Canvas canvas, float posx, float posy) {
    // take offsets into consideration
    posx += getXOffset();
    posy += getYOffset();

    canvas.save();

    // translate to the correct position and draw
    canvas.translate(posx, posy);

    Rect clipBounds = canvas.getClipBounds();
    clipBounds.inset(0, -getHeight());
    canvas.clipRect(clipBounds, Region.Op.INTERSECT);

    draw(canvas);
    canvas.translate(-posx, -posy);

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

如果我将Op更改为Region.Op.Replace,则工具提示会正确绘制,但它会替换工具栏内容,而不是在其下滚动.

在此输入图像描述

android android-custom-view android-canvas mpandroidchart

20
推荐指数
1
解决办法
1544
查看次数