Ich versuche, den Stil auf dynamisch hinzugefügte Schaltflächen in einer Aktivität festzulegen, aber die Schaltflächen sind immer noch mit den Standardeinstellungen versehen.Einstellungsstil für dynamisch hinzugefügte Schaltflächen
Hier ist meine Tätigkeit
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);
}
}
}
und die Layout-Datei für die Aktivität
<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>
und der Stil ich meine Tasten geben möchten:
<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 Datei
<?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>
Sie sind wie diese zeigen:
und ich möchte, dass sie wie folgt aus:
Toll das ist genau das, was ich gesucht habe, um anderen zu helfen, bitte Button ** Button ** Button ** btn ** :) –