【问题标题】:setCompoundDrawablesWithIntrinsicBounds(int,int,int,int) not workingsetCompoundDrawablesWithIntrinsicBounds(int,int,int,int)不起作用
【发布时间】:2013-12-06 12:04:00
【问题描述】:

我有几个EditText views 想在左侧设置图像,而setCompoundDrawablesWithIntrinsicBounds 似乎不起作用。图形似乎没有改变。

有人知道为什么会这样吗?

这是我设置drawables的方式:

        mFirstname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_icon, 0, 0, 0);
        mLastname.setCompoundDrawablesWithIntrinsicBounds(R.drawable.user_icon, 0, 0, 0);
        mEmail.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPassword.setCompoundDrawablesWithIntrinsicBounds(R.drawable.lockicon, 0, 0, 0);
        mDateOfBirth.setCompoundDrawablesWithIntrinsicBounds(R.drawable.calico, 0, 0, 0);
        mCity.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mStreet.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPostcode.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);
        mPhoneNumber.setCompoundDrawablesWithIntrinsicBounds(R.drawable.mailicon, 0, 0, 0);

【问题讨论】:

  • 我在使用单选按钮时遇到了同样的问题。调用根本没有效果,图像总是出现在按钮的右侧。你找到解决办法了吗?
  • 是的!我做到了。抱歉迟了一个星期才回复。所以它似乎会影响带有 ICS 的设备。解决方案发布在下面
  • 下面是什么意思?? @user Denis Glakiy 发布的解决方案?

标签: android xml graphics android-edittext android-drawable


【解决方案1】:

如果其他人有这个看似无法解释的问题,请尝试以下操作:

  • 转到您的 XML 模板。
  • 删除代表它的 XML 图像。
  • 您的视图应该刷新compounddrawable。

基本上,这部分视图似乎不会在 ICS 设备上重新膨胀。希望这能解决一些人的问题!

【讨论】:

    【解决方案2】:

    另一种解决方案是在 UI 循环的下一个(布局后)循环中设置复合可绘制对象:

    final TextView viewById = (TextView) findViewById(R.id.label);
    
    viewById.post(new Runnable()
    {
        @Override
        public void run()
        {
            viewById.setCompoundDrawablesWithIntrinsicBounds(android.R.drawable.ic_menu_call, 0, 0, 0);
        }
    });
    

    【讨论】:

      【解决方案3】:
      mBtnNext=(Button) rootView.findViewById(R.id.btnNext);
      
      Drawable drawableRight=new IconicsDrawable(getActivity().getApplicationContext())
                     .icon(Ionicons.Icon.ion_arrow_right_a)
                      .color(getResources().getColor(R.color.colorBlack))
                      .sizeDp(24);
      
      mBtnNext.setCompoundDrawables(null,null,drawableRight,null);
      

      试试这个你的问题会得到解决的。

      【讨论】:

      • 如果你能解释一下你在那里做了什么会很有帮助
      【解决方案4】:

      当您在 xml 布局中定义了 Drawable(上、左、右、下)图像时,就会出现问题。就像在代码中定义的那个之后应用了 xml drawable。删除 xml 并在任何你想要的代码中执行它。

      【讨论】:

        【解决方案5】:

        我使用了 Ionicons 插件。转到以下链接。您将了解如何在您的 android 项目中使用 IonIcons。

        IonIcons For Android

        在您的项目中实现上述 Ionicons 插件后。您将获得 IconicsDrawable 类。

        Gradle image

        See here to know how to drive IconicsDrawable class in your class

        mBtnNext=(Button) rootView.findViewById(R.id.btnNext);
        
        Drawable drawableRight=new IconicsDrawable(getActivity().getApplicationContext())
                       .icon(Ionicons.Icon.ion_arrow_right_a)
                        .color(getResources().getColor(R.color.colorBlack))
                        .sizeDp(24);
        
        mBtnNext.setCompoundDrawables(null,null,drawableRight,null);
        

        我希望你现在明白了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2012-12-15
          • 1970-01-01
          • 1970-01-01
          • 2017-09-02
          • 2012-09-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多