Android 如何使用Viewpager和片段分割窗口?
我想做这样的东西: 如您所见,ViewPager位于顶部,窗口显示在一个ViewPager和一个片段中 我尝试过以下方法: 活动\u homescreen.xml:Android 如何使用Viewpager和片段分割窗口?,android,android-layout,android-activity,android-fragments,android-viewpager,Android,Android Layout,Android Activity,Android Fragments,Android Viewpager,我想做这样的东西: 如您所见,ViewPager位于顶部,窗口显示在一个ViewPager和一个片段中 我尝试过以下方法: 活动\u homescreen.xml: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity" tools:ignore="MergeRootFrame"
>
<android.support.v4.view.ViewPager
android:id="@+id/home_screen_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<fragment
android:id="@+id/homescreen_fragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup"/>
</LinearLayout>
public class HomeScreenActivity extends Activity{
ViewPager homescreen_viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
homescreen_viewpager = (ViewPager) findViewById(R.id.home_screen_view_pager);
HomeScreenImageAdapter adapter = new HomeScreenImageAdapter(this);
homescreen_viewpager.setAdapter(adapter);
}
public class HomeScreenFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homescreen, container, false);
return rootView;
}
}
}
HomeScreenFragment.java:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity" tools:ignore="MergeRootFrame"
>
<android.support.v4.view.ViewPager
android:id="@+id/home_screen_view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<fragment
android:id="@+id/homescreen_fragment"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="signup"/>
</LinearLayout>
public class HomeScreenActivity extends Activity{
ViewPager homescreen_viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
homescreen_viewpager = (ViewPager) findViewById(R.id.home_screen_view_pager);
HomeScreenImageAdapter adapter = new HomeScreenImageAdapter(this);
homescreen_viewpager.setAdapter(adapter);
}
public class HomeScreenFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_homescreen, container, false);
return rootView;
}
}
HomeScreenImageAdapter.java
public class HomeScreenImageAdapter extends PagerAdapter {
private Context context;
private int[] HomeScreenImages = new int[] {
android.R.drawable.ic_menu_report_image,
android.R.drawable.ic_btn_speak_now,
android.R.drawable.ic_dialog_dialer,
};
public HomeScreenImageAdapter(Context context)
{
this.context = context;
}
@Override
public int getCount() {
return HomeScreenImages.length;
}
@Override
public boolean isViewFromObject(View view, Object object) {
return (view == ((ImageView)object));
}
@Override
public Object instantiateItem (ViewGroup container, int position){
ImageView imageview = new ImageView(context);
imageview.setImageResource(HomeScreenImages[position]);
imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
((ViewPager)container).addView(imageview);
return imageview;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
但是,我看不到碎片下面的m。仅显示ViewPager。您是否知道我们可以使用Viewpager中的屏幕分割和如上所述的片段?可能还有其他问题,但首先,为什么不使用LinearLayout作为容器,并使用适当的相等权重
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeScreenActivity"
tools:ignore="MergeRootFrame" >
<android.support.v4.view.ViewPager
android:id="@+id/home_screen_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<fragment
android:id="@+id/homescreen_fragment"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
</LinearLayout>
我对您主页上的xml感到困惑:“活动\主屏幕”。 您对父布局使用相对布局,但也为两个子布局添加了参数“权重”,其中一个子布局使用0dp宽度。 是否要使用“权重”为viewPager和片段分配空间 我添加和更改了一些代码,并在下面显示。我将父布局更改为LinearLayout并添加“weightSum”。 对于子布局,我为高度指定0dp,为宽度指定与父布局匹配的宽度,并为权重指定高度。您可以更改参数以适合您的设计 我没有尝试过,所以我不确定它是否会起作用。你可以试试
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeScreenActivity" tools:ignore="MergeRootFrame"
android:weightSum="6"
>
<android.support.v4.view.ViewPager
android:id="@+id/home_screen_view_pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"/>
<fragment
android:id="@+id/homescreen_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
class="com.krupalandharsh.touristguideexperiments.HomeScreenFragment" />
</LinearLayout>