लक्ष्य स्क्रीन अभिविन्यास (चित्र या परिदृश्य) के आधार पर एक अलग लेआउट में स्क्रीन पर दो टुकड़े (Frag ए, Frag B) प्रदर्शित करना है।एंड्रॉइड: आप विभिन्न डिवाइस उन्मुखताओं के लिए गैर-व्यूपेजर लेआउट के साथ व्यूपेजर लेआउट को कैसे मिलाते हैं?
जब डिवाइस पोर्ट्रेट मोड में होता है, तो केवल एक ही फ्रेग ए और फ्रैग बी को यूआई के बीच स्विच करने के लिए स्वाइपिंग का उपयोग करके प्रदर्शित किया जाता है। ViewPager इसके लिए पूरी तरह से काम करता है।
जब डिवाइस लैंडस्केप मोड में है, तो स्क्रीन के बाईं ओर Frag A प्रदर्शित होता है और Frag B दाईं ओर प्रदर्शित होता है। एक सरल LinearLayout इसके लिए पूरी तरह से काम करता है।
समस्या एक ही ऐप्लिकेशन में काम करने की कोशिश करते समय उत्पन्न होती है। ऐसा लगता है कि व्यूपेजर क्लास को जावा स्रोत कोड (ऑनक्रेट विधि में) में संदर्भित किया जाना चाहिए। इसका अर्थ यह है कि एक यूआई तत्व को एक्सएमएल और स्रोत कोड दोनों में परिभाषित किया गया है ... जो डिवाइस लैंडस्केप मोड में है जब समस्या की तरह लगता है और वहां ViewPager ऑब्जेक्ट नहीं होना चाहिए, लेकिन किसी को स्रोत कोड में संदर्भित किया जाता है। क्या मैं इसे सही तरीके से देख रहा हूं? यदि एक लेआउट ViewPager का उपयोग कर रहा है, तो क्या हर दूसरे लेआउट को ViewPager की आवश्यकता होती है?
लेआउट/activity_mymain.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyMainActivity" >
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
लेआउट भूमि/activity_mymain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<fragment android:name="com.example.ActionFragment"
android:id="@+id/action_fragment"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
<fragment android:name="com.example.SummaryFragment"
android:id="@+id/summary_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
MyMainActivity.java
सेprotected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_topmain);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}