को AttributeSet पारित करने के लिए मैं कैसे एक कस्टम दृश्य वर्ग के लिए वर्तमान AttributeSet पारित करते हैं? अगर मैं ऐसे कन्स्ट्रक्टर का उपयोग करता हूं जिसमें केवल तर्कों में संदर्भ है, तो मैं उस कस्टम व्यू के लिए xml में "शैली" टैग का उपयोग करने की सभी थीम और क्षमता खो देता हूं।कैसे कस्टम देखें
मैंने क्या किया गया है प्रोग्राम के रूप में एक गतिविधि xml फ़ाइल में पहले से ही अपने कस्टम दृश्य जिसमें बनाते हैं और फिर एक नया बना और लेआउट में जोड़ने के लिए है। मुझे लगता है कि एक्सएमएल में बनाया गया एक उचित स्टाइल है, और जिसे मैं प्रोग्रामेटिक रूप से बनाता हूं वह नहीं करता है।
दोनों के बीच अंतर जहाँ तक मैं बता सकता है कि प्रणाली CustomLayout1(Context context, AttributeSet attrs)
निर्माता का उपयोग करता है। समस्या यह है कि जब मैं इसे प्रोग्रामेटिक रूप से बनाता हूं तो इस कस्टम व्यू को पास करने के लिए एप्लिकेशन के लिए AttributeSet को कैसे प्राप्त किया जाए, यह पता नहीं लगा सकता है।
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="@+id/mainlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.clearsync.test.theme1.CustomLayout1 android:id="@+id/maincustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
कस्टम दृश्य वर्ग:
import com.clearsync.test.theme1.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
public class CustomLayout1 extends LinearLayout {
private Context context = null;
public CustomLayout1(Context context) {
super(context);
this.context = context;
create();
}
public CustomLayout1(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
create();
}
private void create(){
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.inflateme, this, true);
}
}
और अंत में, कस्टम दृश्य xml:
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
public class ThemeOne extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout layout = (LinearLayout) findViewById(R.id.mainlayout);
layout.addView(new CustomLayout1(getApplicationContext()));
}
}
यहां मुख्य एक्सएमएल है:
यहाँ गतिविधि है
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Oh, Hewroh..."
style="?textview_style1" />
</LinearLayout>
सरल बनाएं। सुरुचिपूर्ण। धन्यवाद। मुझे अपने बारे में सोचना चाहिए था :- पी यह एक बहुत ही कष्टप्रद व्यवहार है जिसे मैं निश्चित रूप से देखना पसंद करूंगा। मैंने अपने मुख्य कोड में उस समाधान को लागू किया है, और निश्चित रूप से कई अन्य संबंधित मुद्दों को उजागर किया है। ऐसा लगता है कि एक बग की तरह लगता है कि थीम और स्टाइल बस इतना ही रहस्यमय तरीके से गायब हो जाएगा क्योंकि जिस तरह से आप कस्टम व्यू कहते हैं। * श्वास * – atraudes
[कस्टम दृश्य की प्रतिलिपि बनाने के लिए लेआउट इन्फ्लैटर का उपयोग करने का एक पूर्ण उदाहरण यहां दिया गया है।] (http://stackoverflow.com/a/41500409/3681880) – Suragch