我想做这样的事情:
正如您所看到的,ViewPager位于顶部,窗口在Viewpager和Fragment中出现。
我试过以下:
activity_homescreen.xml :
<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>
fragment_homescreen.xml :
<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>
HomeScreenActivity.java :
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);
}
} 的 HomeScreenFragment.java :
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);
}
}
但是,我看不到下面的片段。只显示ViewPager。您有什么想法我们可以在Viewpager和Fragment中分割屏幕吗?
答案 0 :(得分:2)
也可能存在其他问题,但首先,为什么不使用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>
答案 1 :(得分:0)
我对主页的xml感到困惑:&#34; activity_homescreen&#34;。 您为父布局使用相对布局,但您还添加参数&#34; weight&#34;对于两个子布局,其中一个使用0dp宽度。 你想使用&#34; weight&#34;为viewPager和fragment分配空间?
我添加并更改了一些代码并在下面显示。我将父布局更改为LinearLayout并添加&#34; weightSum&#34;。 对于子布局,我给出了0dp的高度,match_parent的宽度和权重来分配它们的高度。您可以调整参数以适合您的设计。
我没有尝试,所以我不确定它会起作用。你可以尝试一下。
<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>