【问题标题】:Button and drawable left按钮和可绘制的左侧
【发布时间】:2012-05-14 09:09:15
【问题描述】:

我像这样在 xml 文件中创建按钮:

    <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。我知道这很愚蠢,但我需要在活动中执行此操作,因为我在那里创建了按钮。我如何在活动中的 xml 文件中做同样的事情?我需要添加drawableLeft和drawable padding和padding left。 这就是我在活动中创建按钮的方式

 Button button1 = new Button(this);
 button1.setLayoutParams(new RelativeLayout.LayoutParams(buttonWidth, buttonHeight));
 button1.setText(systemTexts.getShowCallButton());
 button1.setBackgroundDrawable(new                                      
 button1.setTextColor(Color.parseColor(buttonTextColor));

【问题讨论】:

    标签: android android-layout


    【解决方案1】:
    Drawable image = getContext().getResources().getDrawable( R.drawable.icon );
    image.setBounds( 0, 0, 60, 60 );
    button.setCompoundDrawables( image, null, null, null );
    

    这样做

    更新:

    由于 getContext().getResources().getDrawable 现在已弃用,请改用它:

      Drawable image = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
      image.setBounds( 0, 0, 60, 60 );
      button.setCompoundDrawables( image, null, null, null );
    

    【讨论】:

      【解决方案2】:

      试试这个,

      Drawable icon= getContext().getResources().getDrawable(R.drawable.icon);
      button.setCompoundDrawablesWithIntrinsicBounds(icon, null, null, null);
      

      【讨论】:

        【解决方案3】:

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

        您可以使用setPadding 设置填充

        文档显示所有 xml 标记的相应 java 方法。

        【讨论】:

          【解决方案4】:

          你可以试试这个:

          txtVw.setCompoundDrawables(R.drawable.img_nature, 0, 0, 0);
          

          【讨论】:

            【解决方案5】:

            你可以使用下面的代码

            <Button android:text="@string/button_label" 
            android:id="@+id/buttonId"
            android:layout_width="160dip"
            android:layout_height="60dip"
            android:layout_gravity="center"
            android:textSize="13dip"
            android:drawableLeft="@drawable/button_icon"
            android:drawablePadding="2dip"
            android:paddingLeft="30dip"
            android:paddingRight="26dip"
            android:singleLine="true"
            android:gravity="center" />  
            

            它将通过填充 ti 文本和图像来管理

            【讨论】:

            • 我认为您没有回答这个问题,OP 想以编程方式进行。
            猜你喜欢
            • 1970-01-01
            • 2017-01-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-02-28
            • 1970-01-01
            相关资源
            最近更新 更多