相关文章推荐
想出国的钱包  ·  aapt.exe已停止工作 ...·  1 周前    · 
跑龙套的小笼包  ·  rk3568 ...·  4 小时前    · 
机灵的皮带  ·  c# ...·  1 年前    · 
眼睛小的香瓜  ·  typeorm update where ...·  1 年前    · 
失恋的马铃薯  ·  python - Django 3.1 ...·  1 年前    · 
android:id="@+id/bSearch" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="16dp" android:text="Search" android:textSize="24sp" />

我试图创建一个带有文字和图标的按钮。 android:drawableLeft对我不起作用(也许可以,但我不知道如何给图标设置一个最大高度)。

所以我创建了一个带有ImageView和TextView的LinearLayout,并让它像一个按钮一样行动。

    <LinearLayout
        android:id="@+id/bSearch2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@android:drawable/btn_default"
        android:clickable="true"
        android:padding="16dp"
        android:orientation="horizontal" >
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_marginLeft="5dp"
            android:adjustViewBounds="true"
            android:maxHeight="30dp"
            android:maxWidth="30dp"
            android:scaleType="fitCenter"
            android:src="@drawable/search_icon" />
        <TextView
            android:id="@+id/tvSearchCaption"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textSize="24sp"
            android:paddingRight="30dp"
            android:gravity="center"
            android:text="Search" />
    </LinearLayout>

我的新按钮正是我想要的(字体大小、图标和文字位置)。 但它看起来不象我的默认按钮。

所以我试图改变我的新Button的背景和文本颜色。

Button Search = (Button) findViewById(R.id.bSearch);
LinearLayout bSearch2 = (LinearLayout) findViewById(R.id.bSearch2);
bSearch2.setBackground(bSearch.getBackground());
TextView tvSearchCaption = (TextView)findViewById(R.id.tvSearchCaption);
tvSearchCaption.setTextColor(bSearch.getTextColors().getDefaultColor());

这产生了一个奇怪的结果,我的旧按钮,被打乱了。

当我改变这两个按钮在XML中的顺序,让 "新按钮 "放在前面时,会产生另一个奇怪的结果。

现在我注意到,当我试图按下旧的按钮时,新的按钮被按下。

有什么想法吗?

android
android-layout
android-button
Dusan
Dusan
发布于 2014-04-09
9 个回答
Liem Vo
Liem Vo
发布于 2022-06-16
已采纳
0 人赞同

Try this one.

<Button
    android:id="@+id/bSearch"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    android:text="Search"
    android:drawableLeft="@android:drawable/ic_menu_search"
    android:textSize="24sp"/>
    
这看起来正是我想要的,但我想使用我自己的图标,而不是默认的安卓图标。我的图标尺寸比较大。这是否意味着,我需要为每个分辨率创建更小的位图?是否有一些参数可以让我控制图标的大小?
这是正确的。你需要为每个分辨率创建不同的图像大小
好的,谢谢你的帮助,但现在我有另一个问题......我的图标是浅色的,因为文字是在深色按钮上的白色,就像你在我的问题中看到的图片。这个应用程序被设置为使用安卓版本中可用的一个主题。我的例子是在安卓4.3版本。当应用程序在安卓2.3.3版本上运行时,默认的按钮是浅色的,深色的文字。有点像我第一张图片上的按钮。是否有办法为在旧设备上运行的图标提供一个替代的位图。我希望你能理解我的问题:)
这是个好问题。你可以为你的图标定义不同的风格,每个API级别(指安卓操作系统版本)的文本冷却器。你可以看到 values-v11, values-v14 文件夹。
你也可以为图标添加填充物 android:drawablePadding=""
user3515854
user3515854
发布于 2022-06-16
0 人赞同

要将图像添加到左边、右边、上面或下面,你可以使用这样的属性。

android:drawableLeft
android:drawableRight
android:drawableTop
android:drawableBottom

上面给出了示例代码。你也可以使用相对布局来实现这一点。

我如何在程序上做到这一点
你可以说android:drawableStart而不是左,android:drawableEnd而不是右。
@SimãoGarcia *应该。在所有的地方,开始应取代右,结束应取代左。这是为了确保从右到左的阅读以及从左到右的阅读,因为其他一些国家的阅读方式不同。
Ky -
@CarsonJ.这是正确的,但你把方向弄反了 🙂把left改为start,把right改为end
Gabriele Mariotti
Gabriele Mariotti
发布于 2022-06-16
0 人赞同

你可以使用材料组件库和MaterialButton组件。
Use the app:iconapp:iconGravity="start"属性。

Something like:

  <com.google.android.material.button.MaterialButton
        style="@style/Widget.MaterialComponents.Button.Icon"
        app:icon="@drawable/..."
        app:iconGravity="start"
    
请注意,如果你的应用程序有一个材料设计主题,标准的Button小部件会自动膨胀为MaterialButton。按这里 -material.io/develop/android/components/buttons
你如何使用一个没有文字,只有Icon的材料设计按钮?
很好,谢谢你!
这有更好的行为,因为Button的状态(禁用/启用等)也会影响图标,无论是在android:drawableLeft的解决方案中--图标总是有一个状态。
Kennedy Nyaga
Kennedy Nyaga
发布于 2022-06-16
0 人赞同

对于任何想动态地做这件事的人来说,按钮对象上的setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom)会有帮助。

Button search = (Button) findViewById(R.id.yoursearchbutton);
search.setCompoundDrawablesWithIntrinsicBounds('your_drawable',null,null,null);
    
Aldo
使用setCompoundDrawables并不能反映在用户界面上。用setCompoundDrawablesWithIntrinsicBounds代替,就可以了。
Stackoverflower
Stackoverflower
发布于 2022-06-16
0 人赞同

这就是你真正想要的东西。

<Button
       android:id="@+id/settings"
       android:layout_width="190dp"
       android:layout_height="wrap_content"
       android:layout_gravity="center_horizontal"
       android:background="@color/colorAccent"
       android:drawableStart="@drawable/ic_settings_black_24dp"
       android:paddingStart="40dp"
       android:paddingEnd="40dp"
       android:text="settings"
       android:textColor="#FFF" />
    
Chamin Wickramarathna
Chamin Wickramarathna
发布于 2022-06-16
0 人赞同

如果你使用的是android.widget.Button而没有进行任何覆盖,那么@Liem Vo的答案是正确的。如果你正在使用MaterialComponents覆盖你的主题,这将不能解决这个问题。

因此,如果你是

  • Using com.google.android.material.button.MaterialButton
  • Overriding AppTheme using MaterialComponents
  • Use 应用程序:图标参数。

    <Button
        android:id="@+id/bSearch"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:text="Search"
        android:textSize="24sp"
        app:icon="@android:drawable/ic_menu_search" />
        
    Papel
    Papel
    发布于 2022-06-16
    0 人赞同

    像这样呢?

    <Button
                android:id="@+id/yourID"
                android:drawableStart="@drawable/ic_flag"
                android:textColor="#FFFFFF"
                android:textSize="12sp"
                android:textStyle="bold"
                android:textAlignment="textStart"
                android:background="@drawable/btn_background"
                android:fontFamily="@font/YourFont"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Your Text" />
        
    Someone
    Someone
    发布于 2022-06-16
    0 人赞同

    try It:-

    position-> left,right,top,bottom
      android:drawableposition="@android:drawable/-yourImage-"
      android:drawabletop="-Yourfilesrc-"
      android:drawablebottom="-Yourfilesrc-"
      android:drawableleft="-Yourfilesrc-"
      android:drawableright="-Yourfilesrc-"
        
    Egor
    Egor
    发布于 2022-06-16
    0 人赞同

    我在纯Button视图中也遇到了同样的问题。在我的情况下,XML的解决方案是没有用的,因为我有一些逻辑来显示/隐藏这个图标。

    There is one more solution using pure Kotlin:

    (bSearch as? MaterialButton)?.let {