how to change a drawableLeft icon size on a button?
46714 просмотра
5 ответа
I added some icons on 4 buttons and they look so big next to the button. So how can I resize them? I used drawableLeft on the buttons to add icons.
The drawables are called deal, trophy, puzzle and megaphone. The icons are too big. The content_main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="tr.k12.evrim.evrimnews.MainActivity"
tools:showIn="@layout/activity_main">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/frameLayout"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Giriş Yap"
android:onClick="SignIn"
android:textStyle="bold"
style="@style/Widget.AppCompat.Button.Colored"/>
</LinearLayout>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="@id/frameLayout"
android:orientation="vertical">
<Button
android:text="Duyurular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="@+id/frameLayout"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:id="@+id/button2"
android:drawableLeft="@drawable/megaphone" />
<Button
android:text="Kadromuz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_below="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:layout_marginTop="13dp"
android:id="@+id/button3"
android:drawableLeft="@drawable/puzzle"/>
<Button
android:text="Başarılarımız"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="12dp"
android:id="@+id/button5"
android:drawableLeft="@drawable/trophy"/>
<Button
android:text="Ortaklarımız"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginTop="12dp"
android:id="@+id/button4"
android:drawableLeft="@drawable/deal"/>
</LinearLayout>
</RelativeLayout>
Автор: Arda Çebi
Источник
Размещён: 13.11.2019 11:35
Ответы (5)
142 плюса
Wrap your resource in a drawable that defines your desired size similar to:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/icon"
android:width="@dimen/icon_size"
android:height="@dimen/icon_size"
/>
</layer-list >
After that, use this drawable in your android:drawableLeft
tag
6 плюса
You can set bounds programatically (here), this will look like this
Drawable img = ActivityName.this.getContext().getResources().getDrawable(
R.drawable.blue_line);
img.setBounds(left, top, right, bottom);
button.setCompoundDrawables(img, null, null, null);
Автор: Orbite
Размещён: 06.12.2016 03:35
6 плюса
Adding to DroidBender's great answer. His solution does not crash on API 21, so it still can be used, even without adding Build.VERSION.SDK_INT
code. This is how the solution using a standard image asset looks on API 21 (using text height as the dimension setting).
And this is how it looks on API 23+
It is still a really good option, even if the min API < 23.
Автор: seekingStillness Размещён: 28.01.2018 01:365 плюса
Try the following. Declare the drawables as the icons you want to use
Drawable drawable = getResources().getDrawable(R.mipmap.youricon);
drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
(int) (drawable.getIntrinsicHeight() * 0.6));
ScaleDrawable sd = new ScaleDrawable(drawable, 0, 40, 40);
youredittext1.setCompoundDrawables(null, null, sd.getDrawable(), null);
drawable = getResources().getDrawable(R.mipmap.yourothericon);
drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * 0.6),
(int) (drawable.getIntrinsicHeight() * 0.6));
sd = new ScaleDrawable(drawable, 0, 40, 40);
youredittext2.setCompoundDrawables(null, null, sd.getDrawable(), null);
Автор: revipod
Размещён: 06.12.2016 03:33
1 плюс
As long as the icon is a Vector asset, go inside the vector file and modify android:width
and android:height
to something like @dimen/icon_size
. Then define this resource in your dimens.xml files for multiple screen sizes. Works like a charm
Вопросы из категории :
- android Насколько хорошо отражает эмулятор Android Phone?
- android Как сохранить состояние активности Android с помощью сохранения состояния экземпляра?
- android Android: доступ к дочерним представлениям из ListView
- android Как вызвать SOAP веб-сервис на Android
- android Как вы форматируете дату и время в Android?
- android Android: Получение имени файла с камеры?
- android Перезапуск активности при ротации Android
- android Проблема с нехваткой памяти при загрузке изображения в растровый объект
- android Отправьте приложение с базой данных
- android Как удалить контакт программно в Android
- android How do I display the current value of an Android Preference in the Preference summary?
- android Ленивая загрузка изображений в ListView
- android Отключить ландшафтный режим в Android?
- android Отображение изображения по имени файла
- android Как проверить, работает ли сервис на Android?
- android Перейти на экран настроек
- android Сохранить растровое изображение в местоположении
- android Android: обновление списка после того, как поток загружает данные из сети
- android Как установить тайм-аут HttpResponse для Android в Java
- android Событие LongClick также вызывает событие Click