gpt4 book ai didi

android - PagerTabStrip 缺少选项卡导航面板

转载 作者:行者123 更新时间:2023-11-30 01:13:30 27 4
gpt4 key购买 nike

我现在正在学习创建选项卡式 Activity ,并且我选择了 PagerTabStrip 来使用。但是现在当我创建 fragment ,将它们分配给适配器,并将适配器分配给 ViewPager 时,PagerTabStrip 的 Tab 导航面板丢失了

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.player.pagertabstrip.MainActivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/viewPager">
<android.support.v4.view.PagerTabStrip
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</LinearLayout>

PageFragment.java

import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class PageFragment extends Fragment {
private static final String ARG_PAGE_NUMBER = "page_number";

public PageFragment() {
}

public static PageFragment newInstance(int page) {
PageFragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE_NUMBER, page);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_page_layout, container, false);

TextView txt = (TextView) rootView.findViewById(R.id.page_number_label);
int page = getArguments().getInt(ARG_PAGE_NUMBER, -1);
txt.setText(String.format("Page %d", page));

return rootView;
}
}

fragment_page_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.player.pagertabstrip.PageFragment">

<TextView android:id="@+id/page_number_label"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="@android:style/TextAppearance.Large"
android:gravity="center"
android:layout_centerVertical="true" />

</RelativeLayout>

MainActivity.java

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TabsPagerAdapter tabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPager.setAdapter(tabsPagerAdapter);
}
}

和无标签导航的截图:-

enter image description here

最佳答案

所以,它似乎是 com.android.support:appcompat-v7:24.0.0 库中的错误。我找到了 2 种方法来解决这个问题。

First:

您必须降级项目中使用的库。在你的 gradle 文件中,将 compile 'com.android.support:appcompat-v7:24.0.0' 替换为 compile 'com.android.support:appcompat-v7:23.2.0'

Second:

您可以通过其父级的 LayoutParams 访问来更改子 PagerTabStrip 的属性

((ViewPager.LayoutParams) pagerTabStrip.getLayoutParams()).isDecor = true;

关于android - PagerTabStrip 缺少选项卡导航面板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38183269/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com