我遇到了android TabLayout的棘手问题
import android.support.design.widget.TabLayout;
Run Code Online (Sandbox Code Playgroud)
当我选择左侧最前面的选项卡,然后向右滚动选项卡并选择右侧最前面的选项卡时,TabLayout首先再次显示左侧选项卡,然后滚动到右侧的选定选项卡.这是我的设置代码
void setupTabs(ViewPager viewPager, TabLayout tabLayout) {
ProductsPagerAdapter adapter = new ProductsPagerAdapter(getChildFragmentManager(), rootCategory.getChildCategories());
viewPager.setAdapter(adapter);
tabLayout.setupWithViewPager(viewPager);
setStartingSelection(viewPager, adapter);
}
private void setStartingSelection(ViewPager viewPager, ProductsPagerAdapter adapter) {
for(int i = 0 ; i < adapter.getCount(); i++){
String title = (String) adapter.getPageTitle(i);
if(title.equals(selectedCategory.getName())){
viewPager.setCurrentItem(i);
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和布局
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/toolbar_color"
app:navigationIcon="@drawable/ic_back_white"
app:title="@string/title_transport"
app:titleTextColor="@color/title_color"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
app:tabTextColor="@color/white"
app:tabIndicatorColor="@color/tab_indicator"
app:tabGravity="fill"/> …Run Code Online (Sandbox Code Playgroud) 我想以编程方式更改选项卡的textview.有没有办法做到这一点?
有关旧TabHost视图的答案,我正在使用Google的Material Design库使用的TabLayout android.support.design.widget.TabLayout.
对于TabHost: 如何将填充添加到选项卡标签?
我有一个带有两个标签的Activity.每个选项卡都包含一个带有a SwipeRefreshLayout和a 的片段RecyclerView.
在活动,我有一个CoordinatorLayout与AppBarLayout(具有Toolbar和一个TabLayout)和一个ViewPager用于片段.
现在,我想要实现的是:当用户滚动片段时,工具栏,而不是标签,隐藏,就像在Play商店中一样.
在我通过Internet阅读的示例中,布局非常简单:它们具有相同xml RecyclerView的Toolbar内部和内部CoordinatorLayout.
然后,他们只写:
<android.support.v7.widget.RecyclerView
android:id="@+id/rvToDoList"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
Run Code Online (Sandbox Code Playgroud)
和
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
所以,我无法弄清楚如何去做.
我的xml如下:
活动布局是:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/activity_main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout 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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/color_primary"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.design.widget.TabLayout
android:id="@+id/activity_main_tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/color_text_primary"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/activity_main_tabs_pager"
android:layout_width="match_parent" …Run Code Online (Sandbox Code Playgroud) android android-fragments android-toolbar android-design-library android-tablayout
自定义始终以Tablayout为中心.tablayout如下.如何获得其父级可用的完整空间,即TabView.
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tablayout"
android:layout_width="match_parent"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
android:layout_height="90dp">
</android.support.design.widget.TabLayout>
Run Code Online (Sandbox Code Playgroud)
添加如下所示的textview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_purple"
android:gravity="center_horizontal">
<TextView
android:text="Hello !"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
但不幸的是,它总是居中,永远不会占据整个空间.问题是我不能在标签之间提供分隔符,因为它始终位于中心.
用于添加标签的Java代码:
TabLayout v = (TabLayout)findViewById(R.id.tablayout);
TabLayout.Tab tab = v.newTab();
tab.setCustomView(R.layout.content_main);
v.addTab(tab);
tab = v.newTab();
tab.setCustomView(R.layout.content_main);
v.addTab(tab);
tab = v.newTab();
tab.setCustomView(R.layout.content_main);
v.addTab(tab)
Run Code Online (Sandbox Code Playgroud)
我分享了这张图片@ http://i60.tinypic.com/11cdvyf.jpg
如何将图像添加到选项卡?目前,我能够标签移至底部,但对如何改变不知道 LL Tab1,LL Tab2,LL Tab3以icon.Am我在正确的道路上?任何帮助将不胜感激.
xml代码
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp"
android:layout_weight="1" >
<LinearLayout
android:id="@+id/ll_tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll_tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
<LinearLayout
android:id="@+id/ll_tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</FrameLayout>
<TabWidget
android:id="@android:id/tabs"
android:background="@color/light_gray"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</TabHost>
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?非常感谢 !
我正在尝试制作布局CollapsingToolbarLayout.但我没有得到一件事.我想删除工具栏和TabLayout之间的阴影.我尝试了几种方法,但我没有设法删除阴影.有谁能够帮我?谢谢
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/adview"
android:fitsSystemWindows="true">
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="50dp"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="bottom"
android:background="?attr/colorPrimary"
android:translationZ="2dp"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom"
app:tabGravity="fill"
app:tabIndicatorColor="@android:color/white"
app:tabIndicatorHeight="3dp"
app:tabMode="fixed"
app:tabTextColor="@color/tabs_text_selector" />
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
android:id="@+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="20dp"
app:expandedTitleMarginStart="20dp"
app:expandedTitleTextAppearance="@style/detalle_txt_expanded"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/detalle_img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:src="@drawable/img_thumb_m"
android:transitionName="@string/transition"
app:layout_collapseMode="parallax"
tools:targetApi="lollipop" />
<ImageView
android:id="@+id/detalle_img_tipo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_gravity="center_horizontal"
android:src="@drawable/img_edificio"/>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar" …Run Code Online (Sandbox Code Playgroud) android toolbar shadow android-collapsingtoolbarlayout android-tablayout
我想要的是.
在选项卡滑动菜单上下文中,我想在选项卡内将片段替换为另一个片段,并维护选项卡菜单以及当前选项卡.当滑动到另一个选项卡并返回原始时,我希望显示最后一个片段.
例如,我有tab_a带Fragment_1,tab_b带Fragment_4和tab_c带Fragment_7.现在,我想在一个按钮,Fragment_1打开了我Fragment_2,但我希望能够左右滑动来的碎片tab_b和tab_c.当我返回tab_a时,Fragment_2必须显示.
MainActivity
|
|
ContainerFragment
|
|
|_ _ _ Tab A
| |_ _ _ Fragment 1
| |
| |_ _ _ Fragment 2
| |
| |_ _ _ Fragment 3
| |
| |_ _ _ ...
|
|_ _ _ Tab B
| |_ _ _ Fragment 4
| |
| …Run Code Online (Sandbox Code Playgroud) java android android-fragments android-viewpager android-tablayout
我似乎无法在我的TabLayout内部将我的标题标题左对齐.目前,标题是中心的.这就是我想要实现的目标.
这就是我现在所拥有的.我正在使用的代码如下:
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabTextColor="@color/white"
app:tabSelectedTextColor="@color/white"
app:tabIndicatorColor="@color/white"
android:background="@color/slate_grey"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.design.widget.TabLayout>
Run Code Online (Sandbox Code Playgroud) 我有一个TabLayout,我希望标签显示在屏幕的中央.下面是我的TabLayout的XML.
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/white"
app:tabGravity="center"
app:tabIndicatorColor="@color/about_tab_selected"
app:tabIndicatorHeight="4dp"
app:tabMode="scrollable"
app:tabPaddingEnd="20dp"
app:tabPaddingStart="20dp"
app:tabSelectedTextColor="@color/about_tab_selected"
app:tabTextAppearance="@style/UGTabTextAppearance"
app:tabTextColor="@color/about_tab_unselected" />
Run Code Online (Sandbox Code Playgroud)
但是,我的标签仍显示在左侧,我无法将它们置于活动中心.
有人可以告诉我这里我做错了什么吗?如果您需要有关XML其余部分的其他信息,请告诉我们.
我正在开发一个需要TabLayout绑定到ViewPager2. 阅读本文并遵循此处的代码,我需要使用TabLayoutMediator. 但是在导入时,我遇到了Unresolved reference错误。我知道这个错误指的是什么,但我在依赖项和导致错误的东西中找不到任何问题。正如第二个链接中提到的,这个类包含在androidx.viewpager2:viewpager2:1.0.0-beta03其中是链接所讨论内容的更新版本。我还尝试将版本降级到链接中提到的版本,但效果不佳。这是我的build.gradle文件:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.myapp"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.core:core-ktx:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation …Run Code Online (Sandbox Code Playgroud)