首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >按钮和可绘制的左侧

按钮和可绘制的左侧
EN

Stack Overflow用户
提问于 2012-05-14 09:09:16
回答 5查看 20K关注 0票数 7

我在xml文件中创建了如下按钮:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <Button
        android:id="@+id/call_button"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/button"
        android:layout_weight="40" 
        android:drawableLeft="@drawable/symbol_phone"
        android:paddingLeft="20dp"
        android:drawablePadding="-25dp"
        android:text="Call"
        android:textColor="@color/white"
        />

我想知道如何在活动中做drawableLeft。我知道这很愚蠢,但我需要在活动中这样做,因为我在那里创建了按钮。我怎样才能在我的活动中做同样的事情呢?我需要添加drawableLeft和可绘制的填充和填充左边。这就是我如何在activity中创建按钮

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 Button button1 = new Button(this);
 button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth, buttonHeight));
 button1.setText(systemTexts.getShowCallButton());
 button1.setBackgroundDrawable(new                                      
 button1.setTextColor(Color.parseColor(buttonTextColor));
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-05-14 09:14:26

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Drawable image = getContext().getResources().getDrawable( R.drawable.icon );
image.setBounds( 0, 0, 60, 60 );
button.setCompoundDrawables( image, null, null, null );

执行此操作

更新:

由于getContext().getResources().getDrawable现在是不推荐使用的,请改用以下代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
  Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
  image.setBounds( 0, 0, 60, 60 );
  button.setCompoundDrawables( image, null, null, null );
票数 20
EN

Stack Overflow用户

发布于 2012-05-14 09:15:15

尝尝这个,

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
Drawable icon= getContext().getResources().getDrawable(R.drawable.icon);
button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
票数 5
EN

Stack Overflow用户

发布于 2012-05-14 09:13:37

以编程方式设置drawableLeft的方法是使用setCompoundDrawablesWithIntrinsicBounds

您可以使用setPadding设置填充

这些文档显示了所有xml标签的相应java方法。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10580380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文