[英]Setting style for dynamically added buttons
I'm trying to set the style to a dynamically added buttons in an activity but the buttons are still with the default stlye. 我正在尝试将样式设置为活动中动态添加的按钮,但这些按钮仍具有默认样式。
Here is my activity 这是我的活动
package com.sample.Learn_German_Quiz;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.view.ViewPropertyAnimatorCompat;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;
import static android.R.interpolator.linear;
public class LessonChooserActivity extends AppCompatActivity {
private GrammarBank grammarBank = new GrammarBank();
public String getSelectedLesson() {
return selectedLesson;
}
public void setSelectedLesson(String selectedLesson) {
this.selectedLesson = selectedLesson;
}
private String selectedLesson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson_chooser);
grammarBank.initLessons(getApplicationContext());
setupButton();
}
private void setupButton() {
Context newContext = new ContextThemeWrapper(getBaseContext(), R.style.button_learn);
for (int i = 0; i <= 5; i++) {
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
Button btn = new Button(newContext);
//Button btn = new Button(this);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}
}
}
and the layout file for the activity 并为活动的布局文件
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="@dimen/fab_margin"
android:id="@+id/linearTest">
<Button
style="@style/button_learn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/ButtonTest"
android:ems="10"
android:text="Test"
android:textColor="#ffff"
android:padding="16dp"
android:onClick="onClick"
/>
</LinearLayout>
and the style I want to give to my buttons: 以及我想给按钮的样式 :
<style name="button_learn" parent="Theme.AppCompat.DayNight.NoActionBar">
<item name="android:background">@drawable/button_rounded</item>
<item name="android:layout_margin">20dp</item>
</style>
button_rounded.xml file button_rounded.xml文件
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="50dp"></corners>
<solid android:color="#4F535F"></solid>
</shape>
</item>
</selector>
They are showing like this: 他们显示如下:
and I want them like this: 我想要他们像这样:
Try this one 试试这个
LinearLayout layout = (LinearLayout) findViewById(R.id.linearTest);
layout.setOrientation(LinearLayout.VERTICAL);
for (int i = 0; i <= 5; i++) {
Button btn= new Button(this, null,R.style.button_learn);
btn.setText(grammarBank.getLessonTitle(i));
layout.addView(btn);
}
Have you tried using another Button constructor that takes also int defStyleAttr? 您是否尝试过使用另一个也带有int defStyleAttr的Button构造函数? If you pass R.style.button_learn as defStyleAttr then it should work.
如果将R.style.button_learn作为defStyleAttr传递,则它应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.