在android.support.design.widget.TabLayout,如何使活动标签始终显示在中心,就像在Play报亭应用程序中一样,如下所示.
第一个和最后一个选项卡也应出现在中心.
我尝试在TabLayout上使用填充.它不起作用.这是我写的代码:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/yellow"
app:tabSelectedTextColor="@android:color/white"
app:tabTextColor="#EEE"
app:tabMode="scrollable"
android:gravity="bottom"
android:clipToPadding="false"
android:paddingLeft="90dp"
android:paddingRight="90dp"
/>
Run Code Online (Sandbox Code Playgroud)
在这种情况下,tabIndicator也将从左移90dp.它应该保持在独立于填充的中心.
android material-design android-tablayout google-play-newsstand
这是我的代码:
private TabLayout tabLayout;
private int[] tabIcons = {
R.mipmap.ic_compass,
R.mipmap.ic_place,
R.mipmap.ic_passport,
R.mipmap.ic_setting
};
...
tabLayout.getTabAt(0).setIcon(tabIcons[0]);
tabLayout.getTabAt(1).setIcon(tabIcons[1]);
tabLayout.getTabAt(2).setIcon(tabIcons[2]);
tabLayout.getTabAt(3).setIcon(tabIcons[3]);
Run Code Online (Sandbox Code Playgroud)
图标的大小取决于图像大小.我怎么能调整它?
Android TabLayout tabPaddingTop和tabPaddingBottom未被删除
请参阅上述问题.
即使我将我的设计库更新为"23.2.0",Tab布局也搞砸了.
下图是我的标签布局.
Xml部分: -
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="@dimen/dp2"
app:tabMode="fixed"
app:tabSelectedTextColor="@android:color/white"
app:tabTextAppearance="@style/MyCustomTabTextAppearance" />
Run Code Online (Sandbox Code Playgroud)
样式xml: -
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/color_156084</item>
</style>
<style name="MyCustomTabTextAppearance" parent="TextAppearance.Design.Tab">
<item name="android:textSize">@dimen/sp14</item>
<item name="android:textColor">@android:color/white</item>
<item name="textAllCaps">false</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我已将padding设置为-1dp,甚至将tabGravity填充,但没有任何工作.
此代码曾用于早期版本,但现在如果我降级它,我在TintManager上得到一个没有类def找到错误.
我有一个示例项目TabLayout和PagerAdapter.奇怪的事情发生了TabLayout当我打电话pagerAdapter.notifyDataSetChanged();后,tabLayout.setupWithViewPager(viewPager);
TabLayout滚动到未知的x位置,因此当前选项卡不可见.但是,如果我向左滚动到期望选项卡,则此选项卡有指示符.
到底是怎么回事?谁能帮助我?我花了太多时间.
代码下方.
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
final SampleFragmentPagerAdapter pagerAdapter = new SampleFragmentPagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
tabLayout.setupWithViewPager(viewPager);
findViewById(R.id.fab).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pagerAdapter.notifyDataSetChanged();
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
我测试了nexus仿真器和nexus真实设备(api 21+) …
请帮忙:导入时出错我android.support.design.widget.TabLayout
说"can not resolve symbol 'design'
我的build.gradle:
compileSdkVersion 26
buildToolsVersion "26.0.0"
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2',
{
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:26'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:26'
testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud) android android-support-library android-gradle-plugin android-design-library android-tablayout
我需要隐藏第一个标签.第一页应该可以工作,但是当用户选择它时,它应该看起来像是在选项卡上.我怎么能这样做?
我找到了TabHost的一些解决方案,对我来说没用.
public class TabFragmentClients extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 5 ;
FinanceClients FinanceClients;
public ClientsFragment clientsFragment;
public FinanceFragment financeFragment;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
*Inflate tab_layout and setup Views.
*/
final View x = inflater.inflate(R.layout.tab_layout_clients,null);
tabLayout = (TabLayout) x.findViewById(R.id.tabs);
viewPager = (ViewPager) x.findViewById(R.id.viewpager);
/**
*Set an Apater for the View Pager
*/
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
/**
* Now , this is a …Run Code Online (Sandbox Code Playgroud) 您好我在我的活动中创建了一个标签布局.这是主要的.xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/main_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xxxxxx.eventmanager.EventDetailsActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/toolbarContainer"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbarContainer"
android:background="?attr/colorPrimary"
android:elevation="6dp"
android:minHeight="?attr/actionBarSize"
app:tabTextColor="@color/colorLight"
app:tabSelectedTextColor="@color/colorAccent"
android:textAlignment=""
android:theme="@style/AppTheme"/>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是我的.java类:
package com.xxxxxx.eventmanager;
import android.graphics.drawable.Drawable;
import android.support.design.widget.TabLayout;
import android.support.v13.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import com.xxxxxx.eventmanager.adapters.EventPagerAdapter;
public class EventDetailsActivity extends AppCompatActivity {
/** …Run Code Online (Sandbox Code Playgroud) 我检查了其他答案和文章.但找不到我要找的东西.我可能会遗漏一些东西.
我有一个标签式活动,其中包含三个片段:
现在我想在单击HomeFragment的特定项目时加载另一个片段("FragmentAnother").说,我想在HomeFragment(Recyclerview片段)中单击"5th"项目时加载"FragmentAnother"(RecyclerViewFragment).我想在HomeFragment中为特定项加载特定的detailView.怎么做到这一点?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager)); …Run Code Online (Sandbox Code Playgroud) adapter android-fragments android-viewpager android-recyclerview android-tablayout
我在 viewPager2 上遇到了这个相当奇怪的问题。我做了一些阅读,发现 viewPager2 的默认偏移限制为 0,这对我的应用程序来说是完美的。我将它与选项卡布局一起使用,并且有 3 个片段(主页、个人资料、通知)。当活动加载并加载第一个片段(主页)时,我可以在我的 logcat 中看到下一个片段(配置文件)未按预期加载。但是当我点击个人资料选项卡时会发生一些奇怪的事情,下一个选项卡(通知)被预加载。调用通知选项卡的方法 onAttach、onCreate、onCreateView、onActivityCreated、onStart。为什么这样做以及如何解决这个问题? Logcat 屏幕截图 我在这里附上了我的 logcat 的屏幕截图。先感谢您。