Android: установить стиль просмотра программно
Вот XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="@style/LightStyle"
android:layout_width="fill_parent"
android:layout_height="55dip"
android:clickable="true"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" />
</RelativeLayout>
Как установить style
приписать программно?
16 ответов
Технически вы можете применять стили программно, в любом случае с пользовательскими представлениями:
private MyRelativeLayout extends RelativeLayout {
public MyRelativeLayout(Context context) {
super(context, null, R.style.LightStyle);
}
}
Конструктор с одним аргументом используется при создании экземпляров представлений программным способом.
Так что цепочка этого конструктора супер, который принимает параметр стиля.
RelativeLayout someLayout = new MyRelativeLayout(context);
Или, как @Dori указал просто:
RelativeLayout someLayout = new RelativeLayout(context, null, R.style.LightStyle);
Что сработало для меня:
Button b = new Button(new ContextThemeWrapper(this, R.style.ButtonText), null, 0);
- Используйте ContextThemeWrapper
А ТАКЖЕ
- Используйте конструктор с тремя аргументами (без этого не получится)
Вы еще не можете установить стиль представления программно, но вы можете найти эту тему полезной.
Обновление: во время ответа на этот вопрос (середина 2012 года, уровень API 14-15), программная настройка представления не была возможной (даже если были некоторые нетривиальные обходные пути), хотя это стало возможным после более позднего API релизы. Смотрите ответ @Blundell для деталей.
Для новой кнопки /TextView:
Button mMyButton = new Button(new ContextThemeWrapper(this, R.style.button_disabled), null, 0);
Для существующего экземпляра:
mMyButton.setTextAppearance(this, R.style.button_enabled);
Для изображения или макетов:
Image mMyImage = new ImageView(new ContextThemeWrapper(context, R.style.article_image), null, 0);
Это довольно старый вопрос, но решение, которое сработало для меня сейчас, - использовать 4-й параметр конструктора defStyleRes
- если есть.. на просмотре... установить стиль
Следующие работы для моих целей (котлин):
val textView = TextView(context, null, 0, R.style.Headline1)
Если вы хотите продолжить использовать XML (что не позволяет принять принятый ответ) и установить стиль после создания представления, вы можете использовать парижскую библиотеку, которая поддерживает подмножество всех доступных атрибутов.
Поскольку вы накачиваете свое представление из XML, вам нужно указать идентификатор в макете:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_styleable_relative_layout"
style="@style/LightStyle"
...
Затем, когда вам нужно изменить стиль программно, после того, как макет был раздут:
// Any way to get the view instance will do
RelativeLayout myView = findViewById(R.id.my_styleable_relative_layout);
// This will apply all the supported attribute values of the style
Paris.style(myView).apply(R.style.LightStyle);
Более того: список поддерживаемых типов и атрибутов вида (включает фон, отступы, поля и т. Д. И может быть легко расширен) и инструкции по установке с дополнительной документацией.
Отказ от ответственности: я первый автор указанной библиотеки.
Вы можете применить стиль к своей деятельности, выполнив:
super.setTheme( R.style.MyAppTheme );
или Android по умолчанию:
super.setTheme( android.R.style.Theme );
в вашей деятельности, до setContentView()
,
Это мой простой пример, ключ ContextThemeWrapper
Оболочка, без нее мой стиль не работает, и используется конструктор с тремя параметрами View.
ContextThemeWrapper themeContext = new ContextThemeWrapper(this, R.style.DefaultLabelStyle);
TextView tv = new TextView(themeContext, null, 0);
tv.setText("blah blah ...");
layout.addView(tv);
Ни один из приведенных ответов не является правильным.
Вы МОЖЕТЕ установить стиль программно.
Краткий ответ: посмотрите на http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/content/Context.java#435
Длинный ответ Вот мой фрагмент, чтобы программно настроить пользовательский стиль на ваш взгляд:
1) Создайте стиль в вашем файле styles.xml
<style name="MyStyle">
<item name="customTextColor">#39445B</item>
<item name="customDividerColor">#8D5AA8</item>
</style>
Не забудьте указать свои пользовательские атрибуты в файле attrs.xml
Мой файл attrsl.xml:
<declare-styleable name="CustomWidget">
<attr name="customTextColor" format="color" />
<attr name="customDividerColor" format="color" />
</declare-styleable>
Обратите внимание, что вы можете использовать любое имя для вашего стиля (мой CustomWidget)
Теперь давайте установим стиль для виджета программно. Вот мой простой виджет:
public class StyleableWidget extends LinearLayout {
private final StyleLoader styleLoader = new StyleLoader();
private TextView textView;
private View divider;
public StyleableWidget(Context context) {
super(context);
init();
}
private void init() {
inflate(getContext(), R.layout.widget_styleable, this);
textView = (TextView) findViewById(R.id.text_view);
divider = findViewById(R.id.divider);
setOrientation(VERTICAL);
}
protected void apply(StyleLoader.StyleAttrs styleAttrs) {
textView.setTextColor(styleAttrs.textColor);
divider.setBackgroundColor(styleAttrs.dividerColor);
}
public void setStyle(@StyleRes int style) {
apply(styleLoader.load(getContext(), style));
}
}
расположение:
<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="22sp"
android:layout_gravity="center"
android:text="@string/styleble_title" />
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</merge>
И, наконец, реализация класса StyleLoader
public class StyleLoader {
public StyleLoader() {
}
public static class StyleAttrs {
public int textColor;
public int dividerColor;
}
public StyleAttrs load(Context context, @StyleRes int styleResId) {
final TypedArray styledAttributes = context.obtainStyledAttributes(styleResId, R.styleable.CustomWidget);
return load(styledAttributes);
}
@NonNull
private StyleAttrs load(TypedArray styledAttributes) {
StyleAttrs styleAttrs = new StyleAttrs();
try {
styleAttrs.textColor = styledAttributes.getColor(R.styleable.CustomWidget_customTextColor, 0);
styleAttrs.dividerColor = styledAttributes.getColor(R.styleable.CustomWidget_customDividerColor, 0);
} finally {
styledAttributes.recycle();
}
return styleAttrs;
}
}
Вы можете найти полностью рабочий пример на https://github.com/Defuera/SetStylableProgramatically
Простой путь проходит через конструктор
RadioButton radioButton = new RadioButton(this,null,R.style.radiobutton_material_quiz);
Я не предлагаю использовать ContextThemeWrapper, поскольку это делает это:
The specified theme will be applied on top of
the base context's theme.
Что может привести к нежелательным результатам в вашем приложении. Вместо этого я предлагаю новую библиотеку "Париж" для этого от ребят из airbnb:
https://github.com/airbnb/paris
Define and apply styles to Android views programmatically
int buttonStyle = R.style.your_button_style;
Button button = new Button(new ContextThemeWrapper(context, buttonStyle), null, buttonStyle);
Только этот ответ работает для меня. См. /questions/8503368/android-kak-programmno-ustanovit-stil-knopki-v-linearlayout/8503382#8503382
Лучшее простое решение, которое я нашел, используя alertDialog с настраиваемым макетом:
val mView = LayoutInflater.from(context).inflate(layoutResId, null)
val dialog = AlertDialog.Builder(context, R.style.CustomAlertDialog)
.setView(mView)
.setCancelable(false)
.create()
где стиль
<style name="CustomAlertDialog" parent="Theme.AppCompat.Light.Dialog.Alert">
<item name="android:background">@drawable/bg_dialog_white_rounded</item>
</style>
и bg_dialog_white_ounded.xml - это
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:radius="16dp" />
<solid android:color="@Color/white" />
</shape>
layoutResId - это идентификатор ресурса любого макета, для которого должна быть установлена тема «@ style / CustomAlertDialog», например:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="@dimen/wdd_margin_medium"
android:theme="@style/CustomAlertDialog"
android:layout_marginEnd="@dimen/wdd_margin_medium">
..... etc...
</androidx.constraintlayout.widget.ConstraintLayout>
если внутри собственного пользовательского представления: val editText = TextInputEditText(context, attrs, defStyleAttr)
Я использовал представления, определенные в XML, в моей составной ViewGroup, надул их и добавил в Viewgroup. Таким образом, я не могу динамически менять стиль, но я могу сделать некоторые настройки стиля. Мой композит:
public class CalendarView extends LinearLayout {
private GridView mCalendarGrid;
private LinearLayout mActiveCalendars;
private CalendarAdapter calendarAdapter;
public CalendarView(Context context) {
super(context);
}
public CalendarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
init();
}
private void init() {
mCalendarGrid = (GridView) findViewById(R.id.calendarContents);
mCalendarGrid.setNumColumns(CalendarAdapter.NUM_COLS);
calendarAdapter = new CalendarAdapter(getContext());
mCalendarGrid.setAdapter(calendarAdapter);
mActiveCalendars = (LinearLayout) findViewById(R.id.calendarFooter);
}
}
и мой взгляд в XML, где я могу назначить стили:
<com.mfitbs.android.calendar.CalendarView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/calendar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<GridView
android:id="@+id/calendarContents"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/calendarFooter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
Вы можете создать XML-файл, содержащий макет с желаемым стилем, а затем изменить фоновый ресурс вашего представления, например, так.