小编Pha*_*inh的帖子

在 setAttributedTitle 之后无法更改 UIButton 标题

我有一个UIButton,我想使颜色的部分UIButton题目,所以我创建NSMutableAttributedString然后setAttributedTitle为按钮。

之后,颜色工作正常,但我无法更改标题 UIButton

如果我不使用setAttributedTitle(通过注释行[self.btnFanLevel setAttributedTitle:text forState:UIControlStateNormal]);),UIButton标题可以改变

这是我用来更改UIButton标题颜色和文本的函数

-(void)changeFanLevel: (NSString *) fanLevelTitle withColor:(UIColor *) color{
    NSLog(@"fanLevelTitle = %@", fanLevelTitle);
    [self.btnFanLevel setTitle:fanLevelTitle forState:UIControlStateNormal];

    NSMutableAttributedString *text =
    [[NSMutableAttributedString alloc]
     initWithAttributedString: self.btnFanLevel.titleLabel.attributedText];

    NSLog(@"text = %@", self.btnFanLevel.titleLabel.attributedText);

    [text addAttribute:NSForegroundColorAttributeName
                 value:color
                 range:NSMakeRange(10, 3)];

    [self.btnFanLevel setAttributedTitle:text forState:UIControlStateNormal];
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么吗?任何帮助,将不胜感激。

更新
我通过在设置标题之前添加这一行来解决我的问题UIButton

[self.btnFanLevel setAttributedTitle:nil forState:UIControlStateNormal];

objective-c uibutton ios

3
推荐指数
1
解决办法
1722
查看次数

获取设置为包装内容的布局高度

我想获得设置为包装内容的线性布局的高度。但它是零。我试试这个代码:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);


         ViewTreeObserver vto = below.getViewTreeObserver();  
         vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {  
             @Override  
             public void onGlobalLayout() {  
                  width  = below.getMeasuredWidth(); 
                  height = below.getMeasuredHeight();  

             }  
         });
Run Code Online (Sandbox Code Playgroud)

和 :

 LinearLayout below= (LinearLayout) findViewById(R.id.below);
       final int belowh=below.getHeight();
Run Code Online (Sandbox Code Playgroud)

使用代码:

 final LinearLayout below= (LinearLayout) findViewById(R.id.below);
        ViewTreeObserver vto = below.getViewTreeObserver(); 
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
            @Override 
            public void onGlobalLayout() { 

        if(below.getMeasuredHeight()> 0){

                 this.below.getViewTreeObserver().removeGlobalOnLayoutListener(this); 
                int width  = below.getMeasuredWidth();
                int height = below.getMeasuredHeight(); 
        }
            } 
        })

    ;
Run Code Online (Sandbox Code Playgroud)

点击这里查看

android android-linearlayout

3
推荐指数
1
解决办法
4441
查看次数

CheckedTextView中的文本无法居中

 <CheckedTextView
            android:id="@+id/charge_industry_textView"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:drawableTop="@drawable/charge3"
            android:gravity="center"
            android:text="@string/ChargeIndustry"
            android:background="@drawable/checkedtextview_checked"/>
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

CheckedTextView当我制作时,文字中的文字无法居中 android:gravity="center"

android android-layout

3
推荐指数
1
解决办法
676
查看次数

Kotlin断线评论不起作用

Java我经常使用<br/>(或另一个html标签)这样的表达,使评论易于阅读

/**
 * Line 1 <br/>
 * Line 2
 */
Run Code Online (Sandbox Code Playgroud)

在Kotlin,我写这样的评论,但它不起作用("第2行"不会破坏新行).有没有办法实现它(任何设置或插件)?

android kotlin android-studio

3
推荐指数
1
解决办法
181
查看次数

CustomLayout 的子视图中出现意外的命名空间前缀应用程序

在 a 的子视图中使用属性时出现错误CustomLayout(我定义了一个自定义 LayoutParams 以允许子视图使用此属性)

在此处输入图片说明

但是,代码仍在运行并显示正确的值(您可以在下面查看我的代码,当我运行应用程序时,它会在 logcat 中显示“Hello”)

这是我的代码 style.xml

<declare-styleable name="CustomRelativeLayout_Layout">
     <attr name="title" format="string" />
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

CustomRelativeLayout 类

public class CustomRelativeLayout extends RelativeLayout {

    public CustomRelativeLayout(Context context) {
        this(context, null);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CustomRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new CustomLayoutParams(getContext(), attrs);
    }

    @Override
    public void onViewAdded(View child) {
        super.onViewAdded(child);
        CustomLayoutParams layoutParams = (CustomLayoutParams) …
Run Code Online (Sandbox Code Playgroud)

android android-custom-view

3
推荐指数
1
解决办法
325
查看次数

IllegalArgumentException:LinearLayoutManager已附加到RecyclerView

IllegalArgumentException:LinearLayoutManager已附加到RecyclerView

我得到这个Exception当我尝试设置LayoutManager为我RecyclerView和它说,LinearLayoutManager is already attached
但在此之前我设置LinearLayoutManager我RecyclerView,我已经检查我的RecyclerView包含LinearLayout或不

如下图所示,您将看到RecyclerView.LayoutManager m= null但仍然会抛出异常

在此输入图像描述

RecyclerView.LayoutManager m = recyclerView.getLayoutManager();
if(recyclerView.getLayoutManager() != null){
      return;
}
try {
      recyclerView.setLayoutManager(layoutManager);
}catch (Exception e){
      Log.e("AA", "setRecyclerViewLayoutManager: ", e);
}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会这样.我怎样才能防止LinearLayoutManager为我设置新的RecyclerView

这是logcat

java.lang.IllegalArgumentException: LayoutManager android.support.v7.widget.LinearLayoutManager@116d9b78 is already attached to a RecyclerView: android.support.v7.widget.RecyclerView{1cbedba4 VFED.... .......D 0,0-1080,1181 #7f0d00b6 app:id/recycler_news}
                                                                    at android.support.v7.widget.RecyclerView.setLayoutManager(RecyclerView.java:1087)
                                                                    at com.toong.map.utils.BindingUtils.setRecyclerViewLayoutManager(BindingUtils.java:76)
                                                                    at com.toong.map.databinding.FragmentNewsBaseBinding.executeBindings(FragmentNewsBaseBinding.java:191)
                                                                    at android.databinding.ViewDataBinding.executePendingBindings(ViewDataBinding.java:355)
                                                                    at android.databinding.ViewDataBinding$6.run(ViewDataBinding.java:172)
                                                                    at android.databinding.ViewDataBinding$7.doFrame(ViewDataBinding.java:238)
                                                                    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:765)
                                                                    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
                                                                    at …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-recyclerview

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

SweepGradient改变开始和结束颜色的位置

我想创建一个带有渐变的CircleView - > left - > top - > right.
所以我用帆布SweepGradient这样

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();

    int[] colors = {Color.GREEN, Color.RED};
    float[] positions = {0, 1};

    SweepGradient gradient = new SweepGradient(100, 100, colors, positions);
    paint.setShader(gradient);
    canvas.drawCircle(100, 100, 100, paint);
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但是这个的默认顺序是正确的 - >底部 - >左边 - >顶部但是我想底部 - >左边 - >顶部 - >右边 我试过改变位置到

float[] positions = {0.25f, 1.25f};
Run Code Online (Sandbox Code Playgroud)

但它只适用于PreviewAndroidStudio,当我在真实设备中运行时,它显示的结果与positions = {0, 1}

我怎样才能像这样制作SweepGradient …

android canvas sweepgradient

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

为什么NSNumber*a = 0不显示错误

我知道初始NSNumber的正确方法是 NSNumber *a = @1;

当我宣布时NSNumber *a = 1;,我会得到错误

使用arc不允许将int隐式转换为nsnumber

但我不知道为什么当我宣布NSNumber *a = 0;没有错误

就我而言,我已经在NSNumber类别中编写了一些函数然后

  • 如果NSNumber的值是@0,我可以正常使用类别中的函数
  • 如果NSNumber的值是0,我可以使用类别中的函数,没有发生错误但是当运行app时,此函数将永远不会调用

objective-c ios

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

以编程方式更改图层列表项的顶部/右侧/底部/左侧

我有一个 layer-list.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
    </item>
    <item
        android:id="+id/item_2"
        android:bottom="1dp"
        android:left="1dp"
        android:right="1dp"
        android:top="1dp">
        <shape android:shape="rectangle">
            <solid android:color="@android:color/white" />
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

现在我想改变项目的一些属性,比如(top,right)在运行时。我可以通过以下方式更改项目内部形状的颜色

LayerDrawable layerDrawable = (LayerDrawable) v.getContext().getResources().getDrawable(R.drawable.layer_list);
GradientDrawable drawable2 = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_2);
drawable2.setColor(strokeColor[0]); 
Run Code Online (Sandbox Code Playgroud)

但是不知道怎么改top,right。我怎样才能改变它?
任何帮助或建议将不胜感激。

android drawable layer-list

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

使用DataBinding在RecyclerView项目中绑定图像时出现闪烁图像

DataBinding在RecyclerView项目中使用绑定图像,如下面的代码,然后当我重新加载所有RecyclerView时使图像闪烁(通过notifyDataSetChange)
如果我不使用DataBinding并且只是正常设置图像,则图像不会闪烁我该怎么办?DataBinding?任何帮助或建议将不胜感激

我的RecyclerView适配器代码

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
    private Context mContext;
    private List<String> list;
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        ItemBinding mBinding;
        public MyViewHolder(ItemBinding mBinding) {
            super(mBinding.getRoot());
            this.mBinding = mBinding;
            mBinding.setViewHolder(this);
        }

        public int getSrc(){
            return R.drawable.ic_launcher;
        }
    }
    public RecyclerViewAdapter(Context context, List<String> moviesList) {
        this.mContext = context;
        this.list = moviesList;
    }
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        ItemBinding binding =
                DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.item,
                        parent, false);
        return new MyViewHolder(binding);
    }
    @Override …
Run Code Online (Sandbox Code Playgroud)

android android-recyclerview android-databinding

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

Android使用矢量可绘制的图像在android &lt;5中的customview中的图像

我有一个CustomView与类TextViewImageView类似

public class CustomView extends LinearLayout {
    private ImageView imgImage;
    ...

    public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
    }
    private void init(AttributeSet attrs) {
        LayoutInflater.from(getContext()).inflate(R.layout.custom_layout, this, true);
        imgImage = (ImageView) findViewById(R.id.image);

        TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CustomViewStyle);
        Drawable drawable = ta.getDrawable(R.styleable.CustomViewStyle_image); // CRASH LINE

        ta.recycle();
        imgImage.setBackground(drawable);
    }
}
Run Code Online (Sandbox Code Playgroud)

style.xml

<declare-styleable name="CustomViewStyle">
     <attr format="string" name="text"/>
     <attr format="reference" name="image"/>
</declare-styleable>
Run Code Online (Sandbox Code Playgroud)

build.gradle

android {
  defaultConfig {
    ...
    vectorDrawables.useSupportLibrary = true
  } …
Run Code Online (Sandbox Code Playgroud)

android android-vectordrawable

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