के साथ प्रयुक्त inflater का उपयोग किया गया है मेरे पास टेक्स्टव्यू के लिए पाठ रंग को लाल रंग के रूप में निर्दिष्ट किया गया है।थीम/स्टाइल लागू नहीं किया गया है जब अनुप्रयोगकॉन्टेक्स्ट
मैं TextView को तुरंत चालू करने के लिए लेआउटइनफ्लेटर का उपयोग कर रहा हूं। समस्या यह है कि अनुप्रयोग दृश्यों का उपयोग करके बनाए गए inflater जब शैलियों को TextView पर लागू नहीं किया जाता है - रंग लाल नहीं होता है। जब सभी लेआउट इन्फ्लेटर गतिविधि का उपयोग करके बनाया गया तो ठीक काम करता है।
ऐसा क्यों होता है, और कैसे तय किया जा सकता है?
/res/values/styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTheme">
<item name="android:textViewStyle">@style/MyTextView</item>
</style>
<style name="MyTextView" parent="@android:style/Widget.TextView">
<item name="android:textColor">#f00</item>
</style>
</resources>
AndroidManifest.xml:
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/MyTheme"
>
कोड:
public class A extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_a);
final LayoutInflater goodInflater = getInflater((Activity)this);
final LayoutInflater badInflater = getInflater(getApplicationContext());
final LinearLayout container = (LinearLayout)findViewById(R.id.container);
findViewById(R.id.add_with_appContext).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add(container, badInflater); // Creates gray TextView
}
});
findViewById(R.id.add_with_activity).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
add(container, goodInflater); // Creates red TextView
}
});
}
private LayoutInflater getInflater(Context context) {
return (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
private void add(LinearLayout container, LayoutInflater inflater) {
inflater.inflate(R.layout.my_template, container, true);
}
}
/res/layout/test_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:text="Add with AppContext"
android:id="@+id/add_with_appContext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:text="Add with Activity"
android:id="@+id/add_with_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<LinearLayout
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
</LinearLayout>
/res/layout/my_template.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/text"
android:text="Some text..."
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
सर में आपने गतिविधि का एक ही विषय रखने के लिए व्यक्तिगत रूप से किया है। सर आपने अपना दिन बचाया। धन्यवाद –
यह इतना गलत है। ऐसा एक कारण है जिस तरह से आप सोचते हैं कि यह काम करता है: यह उस तरह से काम नहीं करना चाहिए। टैग पर थीम विशेषता केवल डिफ़ॉल्ट है जो सभी गतिविधियों पर लागू होती है। उदाहरण के लिए, कई अन्य चीजें वास्तव में ऐप संदर्भ के साथ काम नहीं करती हैं, उदाहरण के लिए, आप संसाधनों को रिसाव करेंगे जो कॉन्फ़िगरेशन परिवर्तनों के जवाब में बदलते हैं, आदि –
dcow
समाधान # 2 एक आकर्षण की तरह काम करता है। मेरा आवेदन अब खूबसूरत लग रहा है। धन्यवाद! – Mauker