【发布时间】:2015-09-03 02:12:36
【问题描述】:
我正在尝试在他们的应用上复制 Twitter 的个人资料视图。
这是视图的图片:
http://i.imgur.com/otgyxi4.png
我正在努力实现 3 个具体的目标:
- 视图顶部的标题布局
- 标题下方的 ListView(在 Twitter 应用上,这是帖子)
- 用于左右滑动的 ViewPager(在 Twitter 应用上,这是“推文”、“照片”和“收藏夹”选项卡
我已经完成了列表中的第一名。这是标题布局(header.xml):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
</LinearLayout>
我将它加载到我的 Fragment 的 onCreateView() 方法中:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
final ListView listView = (ListView) view.findViewById(R.id.listview);
View header = inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
...
return view;
}
这是我的第一个片段,fragment_one.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在我的主要活动布局 (activity_main.xml) 中,我有这个:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.astuetz.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@color/black"
app:pstsIndicatorHeight="2dp"
app:pstsShouldExpand="true"
app:pstsTabTextSize="14dp"
/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/tabs"
/>
</LinearLayout>
我的 Activity 的 onCreate() 方法如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pager = (ViewPager) findViewById(R.id.pager);
pagerAdapter = new PagerAdapter(getSupportFragmentManager());
pager.setAdapter(pagerAdapter);
PagerSlidingTabStrip tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
}
但是,我有两个问题:
- 标签不在标题下方,因为它们应该是。
- 当我滑动到另一个选项卡时,标题也会移动(即使它是所有 3 个片段上的相同标题)。这是我的意思的图片:http://i.imgur.com/GXWgzkm.png
如何解决这些问题?
【问题讨论】:
标签: android android-layout android-fragments android-activity android-xml