Android TableLayout提供了一个类似网格的布局来组织信息的行和列。
如何删除Widget.Material3.TabLayout底部分隔线
对于使用 Widget.Material3.TabLayout 的样式 <item name="tabTextColor">@彩色/黑色&...</desc> <question vote="3"> <p>对于使用 <pre><code>Widget.Material3.TabLayout</code></pre></p> 的样式 <pre><code><style name="MyTabLayout" parent="Widget.Material3.TabLayout"> <item name="tabTextColor">@color/black</item> <item name="tabSelectedTextColor">@color/black</item> <item name="tabIndicatorFullWidth">true</item> </style> </code></pre> <p><a href="https://i.sstatic.net/kWSC0.png" rel="nofollow noreferrer"><img src="https://cdn.txt58.com/i/AWkuc3N0YXRpYy5uZXQva1dTQzAucG5n" alt="enter image description here"/></a></p> <p>如何删除与指示器相邻的灰色底部分隔线,</p> <p><a href="https://i.sstatic.net/MLkpJ.png" rel="nofollow noreferrer"><img src="https://cdn.txt58.com/i/AWkuc3N0YXRpYy5uZXQvTUxrcEoucG5n" alt="enter image description here"/></a></p> <p>只有蓝色指示器可见?</p> </question> <answer tick="false" vote="0"> <p>这对我应用背景很有用,在我的例子中是白色的。 <pre><code><com.google.android.material.tabs.TabLayout android:id="@+id/status_tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabIndicatorColor="@color/transparent" app:tabMode="fixed" app:tabIndicatorFullWidth="false" app:tabIndicatorHeight="0dp" android:background="@color/white"/></code></pre></p> </answer> </body></html>
Android 设计支持库 TabLayout 使用自定义选项卡布局,但布局包裹选项卡
在选项卡自定义布局中,我将其父元素设置为match_parent并设置其背景颜色。当我运行它时,选项卡会显示包装元素 imageview 和 textview 的自定义布局。我想要这个定制
像 Amazon prime 这样的电影应用程序,我会使用 CategoryItem 显示电影类别。我想用Recycler显示所有类别
我把事情搞混了。我遇到的最初错误是 MainActivity.java:130: error: incomplete types: List无法转换为List 我把事情搞混了。我遇到的最初错误是这个 MainActivity.java:130: error: incomplete types: List can be conversion to List allCategoryList.add(new AllCategory(1,"VIP Hour", homeCatListItem1)); 当尝试修复它时,事情开始变得混乱。所以我仍然有这个错误 主要活动 import androidx.appcompat.app.AppCompatActivity; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import androidx.viewpager.widget.ViewPager; import android.os.Bundle; import com.google.android.material.tabs.TabLayout; import com.kuroniczstudio.splashscreen.adapter.BannerMoviesPagerAdapter; import com.kuroniczstudio.splashscreen.adapter.MainRecyclerAdapter; import com.kuroniczstudio.splashscreen.model.AllCategory; import com.kuroniczstudio.splashscreen.model.BannerMovies; import java.util.ArrayList; import java.util.List; import java.util.Timer; import java.util.TimerTask; public class MainActivity extends AppCompatActivity { BannerMoviesPagerAdapter bannerMoviesPagerAdapter; TabLayout indicatorTab, categoryTab; ViewPager bannerMoviesViewPager; List<BannerMovies> homeBannerList; List<BannerMovies> vipHourBannerList; List<BannerMovies> prophecyBannerList; List<BannerMovies> testimonyBannerList; Timer sliderTimer; MainRecyclerAdapter mainRecyclerAdapter; RecyclerView mainRecycler; List<AllCategory> allCategoryList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); indicatorTab = findViewById(R.id.tab_indicator); categoryTab = findViewById(R.id.tablayout); homeBannerList = new ArrayList<>(); homeBannerList.add(new BannerMovies(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c")); homeBannerList.add(new BannerMovies(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA")); homeBannerList.add(new BannerMovies(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM")); vipHourBannerList = new ArrayList<>(); vipHourBannerList.add(new BannerMovies(1, "I will be better than my father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/ymIUteex694")); vipHourBannerList.add(new BannerMovies(3, "I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/Sbbu536E9vA")); vipHourBannerList.add(new BannerMovies(4, "My Hair Will Grow Again", "https://img.yts.mx/assets/images/movies/sharkula_2022/medium-cover.jpg", "https://youtu.be/yau5eQH1K_I")); prophecyBannerList = new ArrayList<>(); prophecyBannerList.add(new BannerMovies(2, "Looking for Jackie", "https://img.yts.mx/assets/images/movies/looking_for_jackie_2009/medium-cover.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k")); prophecyBannerList.add(new BannerMovies(3, "Khuda Haafiz Chapter II", "https://sdmoviespoint.mba/wp-content/uploads/2022/07/Khuda-Haafiz-Chapter-II-Agni-Pariksha-2022-Full-Movie-Download-Free.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k")); prophecyBannerList.add(new BannerMovies(4, "Sharkula", "https://img.yts.mx/assets/images/movies/sharkula_2022/medium-cover.jpg", "https://youtu.be/6SOEYXZK6Q4")); prophecyBannerList.add(new BannerMovies(5, "Looking for Jackie", "https://img.yts.mx/assets/images/movies/looking_for_jackie_2009/medium-cover.jpg", "https://www.youtube.com/watch?v=ByYWL1SEe-k")); testimonyBannerList = new ArrayList<>(); testimonyBannerList.add(new BannerMovies(1, "See how this little boy survived under the water for 2 hours", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/rRFkBLGQIs8")); testimonyBannerList.add(new BannerMovies(2, "Testimony Blessing Nsikak Saved from spiritual attack", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/TdYn24MdD1Q")); testimonyBannerList.add(new BannerMovies(3, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/HNjxxk3eWKk")); //this is default tab selected setBannerMoviesPagerAdapter(homeBannerList); categoryTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { @Override public void onTabSelected(TabLayout.Tab tab) { switch (tab.getPosition()){ case 1: setBannerMoviesPagerAdapter(vipHourBannerList); return; case 2: setBannerMoviesPagerAdapter(prophecyBannerList); return; case 3: setBannerMoviesPagerAdapter(testimonyBannerList); return; default: setBannerMoviesPagerAdapter(homeBannerList); } } @Override public void onTabUnselected(TabLayout.Tab tab) { } @Override public void onTabReselected(TabLayout.Tab tab) { } }); List<CategoryItem> homeCatListItem1 = new ArrayList<>(); homeCatListItem1.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c")); homeCatListItem1.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA")); homeCatListItem1.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM")); homeCatListItem1.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I")); homeCatListItem1.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk")); List<CategoryItem> homeCatListItem2 = new ArrayList<>(); homeCatListItem2.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c")); homeCatListItem2.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA")); homeCatListItem2.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM")); homeCatListItem2.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I")); homeCatListItem2.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk")); List<CategoryItem> homeCatListItem3 = new ArrayList<>(); homeCatListItem3.add(new CategoryItem(1, "Trailer - I cannot give up now!!!", "https://i.pinimg.com/originals/1a/5e/c6/1a5ec63c50513055ac7674ad65c2e1a1.jpg", "https://youtu.be/7w1YKrHW85c")); homeCatListItem3.add(new CategoryItem(2, "Trailer - I must cross the Red Sea", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/88dHJdCyyBA")); homeCatListItem3.add(new CategoryItem(3, "Trailer - I must be better than my Father", "https://i.pinimg.com/originals/ec/60/46/ec604668771e8916466be6835d2ea588.jpg", "https://youtu.be/GsNrJgLSTAM")); homeCatListItem3.add(new CategoryItem(4, "My Hair Will Grow Again", "https://i.pinimg.com/originals/14/f1/5c/14f15cc3bbc780abdfecafa0f94024e1.jpg", "https://youtu.be/yau5eQH1K_I")); homeCatListItem3.add(new CategoryItem(5, "See how this man got back his transformer working again", "https://i.pinimg.com/originals/7f/8f/02/7f8f02fdfc2b09de9d01d86395d128b1.jpg", "https://youtu.be/HNjxxk3eWKk")); allCategoryList = new ArrayList<>(); allCategoryList.add(new AllCategory(1,"VIP Hour", homeCatListItem1)); allCategoryList.add(new AllCategory(2,"VIP Hour", homeCatListItem2)); allCategoryList.add(new AllCategory(3,"VIP Hour", homeCatListItem3)); setMainRecycler(allCategoryList); } private void setBannerMoviesPagerAdapter(List<BannerMovies> bannerMoviesList) { bannerMoviesViewPager = findViewById(R.id.banner_viewPager); bannerMoviesPagerAdapter = new BannerMoviesPagerAdapter(this, bannerMoviesList); bannerMoviesViewPager.setAdapter(bannerMoviesPagerAdapter); indicatorTab.setupWithViewPager(bannerMoviesViewPager); sliderTimer = new Timer(); sliderTimer.scheduleAtFixedRate(new AutoSlider(), 4000, 6000); indicatorTab.setupWithViewPager(bannerMoviesViewPager, true); } class AutoSlider extends TimerTask { @Override public void run() { MainActivity.this.runOnUiThread(() ->{ if (bannerMoviesViewPager.getCurrentItem() < homeBannerList.size() - 1) { bannerMoviesViewPager.setCurrentItem(bannerMoviesViewPager.getCurrentItem() + 1); } else{ bannerMoviesViewPager.setCurrentItem(0); } }); } } public void setMainRecycler(List<AllCategory> allCategoryList) { mainRecycler = findViewById(R.id.main_recycler); RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false); mainRecycler.setLayoutManager(layoutManager); mainRecyclerAdapter = new MainRecyclerAdapter(this, allCategoryList); mainRecycler.setAdapter(mainRecyclerAdapter); } } //模型目录中的项目// 类别项目 public class CategoryItem { Integer id; String movieName; String imageUrl; String fileUrl; public CategoryItem(Integer id, String movieName, String imageUrl, String fileUrl) { this.id = id; this.movieName = movieName; this.imageUrl = imageUrl; this.fileUrl = fileUrl; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMovieName() { return movieName; } public void setMovieName(String movieName) { this.movieName = movieName; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getFileUrl() { return fileUrl; } public void setFileUrl(String fileUrl) { this.fileUrl = fileUrl; } } 所有类别.Class import java.util.List; public class AllCategory { String categoryTitle; Integer categoryId; private List<CategoryItem> categoryItemList = null; public AllCategory(Integer categoryId, String categoryTitle, List<CategoryItem> categoryItemList) { this.categoryTitle = categoryTitle; this.categoryId = categoryId; this.categoryItemList = categoryItemList; } public List<CategoryItem> getCategoryItemList() { return categoryItemList; } public void setCategoryItemList(List<CategoryItem> categoryItemList) { this.categoryItemList = categoryItemList; } public String getCategoryTitle(){ return categoryTitle; } public void setCategoryTitle(String categoryTitle){ this.categoryTitle = categoryTitle; } public Integer getCategoryId() { return categoryId; } public void setCategoryId(Integer categoryId) { this.categoryId = categoryId; } } 横幅电影 包 com.kuroniczstudio.splashscreen.model; 公共课 BannerMovies { Integer id; String movieName; String imageUrl; String fileUrl; public BannerMovies(Integer id, String movieName, String imageUrl, String fileUrl) { this.id = id; this.movieName = movieName; this.imageUrl = imageUrl; this.fileUrl = fileUrl; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getMovieName() { return movieName; } public void setMovieName(String movieName) { this.movieName = movieName; } public String getImageUrl() { return imageUrl; } public void setImageUrl(String imageUrl) { this.imageUrl = imageUrl; } public String getFileUrl() { return fileUrl; } public void setFileUrl(String fileUrl) { this.fileUrl = fileUrl; } } _____________________________________ **MainRecyclerAdapter** package com.kuroniczstudio.splashscreen.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.kuroniczstudio.splashscreen.CategoryItem; import com.kuroniczstudio.splashscreen.R; import com.kuroniczstudio.splashscreen.model.AllCategory; import java.util.List; public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.MainViewHolder> { Context context; List<AllCategory> allCategoryList; public MainRecyclerAdapter(Context context, List<AllCategory> allCategoryList) { this.context = context; this.allCategoryList = allCategoryList; } @NonNull @Override public MainViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new MainViewHolder(LayoutInflater.from(context).inflate(R.layout.main_recycler_row_item, parent)); } @Override public void onBindViewHolder(@NonNull MainViewHolder holder, int position) { holder.categoryName.setText(allCategoryList.get(position).getCategoryTitle()); } @Override public int getItemCount() { return allCategoryList.size(); } public static final class MainViewHolder extends RecyclerView.ViewHolder{ TextView categoryName; RecyclerView itemRecycler; public MainViewHolder(@NonNull View itemView) { super(itemView); categoryName = itemView.findViewById(R.id.item_category); itemRecycler = itemView.findViewById(R.id.item_recycler); } } private void setItemRecycler(RecyclerView recyclerView, List<CategoryItem> categoryItemList){ ItemRecyclerAdapter itemRecyclerAdapter = new ItemRecyclerAdapter(context, categoryItemList); recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.HORIZONTAL, false)); recyclerView.setAdapter(itemRecyclerAdapter); } } ItemRecyclerAdapter package com.kuroniczstudio.splashscreen.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import com.bumptech.glide.Glide; import com.kuroniczstudio.splashscreen.CategoryItem; import com.kuroniczstudio.splashscreen.R; import java.util.List; public class ItemRecyclerAdapter extends RecyclerView.Adapter<ItemRecyclerAdapter.ItemViewHolder> { Context context; List<CategoryItem> categoryItemList; public ItemRecyclerAdapter(Context context, List<CategoryItem> categoryItemList) { this.context = context; this.categoryItemList = categoryItemList; } @NonNull @Override public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { return new ItemViewHolder(LayoutInflater.from(context).inflate(R.layout.cat_recycler_row_items,parent, false)); } @Override public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { Glide.with(context).load(categoryItemList.get(position).getImageUrl()).into(holder.itemImage); } @Override public int getItemCount() { return categoryItemList.size(); } public static final class ItemViewHolder extends RecyclerView.ViewHolder{ ImageView itemImage; public ItemViewHolder(@NonNull View itemView) { super(itemView); itemImage = itemView.findViewById(R.id.item_image); } } } BannerMoviesPagerAdapter package com.kuroniczstudio.splashscreen.adapter; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import androidx.annotation.NonNull; import androidx.viewpager.widget.PagerAdapter; import com.bumptech.glide.Glide; import com.kuroniczstudio.splashscreen.R; import com.kuroniczstudio.splashscreen.model.BannerMovies; import java.util.List; public class BannerMoviesPagerAdapter extends PagerAdapter { Context context; List<BannerMovies> bannerMoviesList; public BannerMoviesPagerAdapter(Context context, List<BannerMovies> bannerMoviesList) { this.context = context; this.bannerMoviesList = bannerMoviesList; } @Override public int getCount() { return bannerMoviesList.size(); } @Override public boolean isViewFromObject(@NonNull View view, @NonNull Object object) { return view == object; } @Override public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) { container.removeView((View) object); } @NonNull @Override public Object instantiateItem(@NonNull ViewGroup container, int position) { View view = LayoutInflater.from(context).inflate(R.layout.banner_movie_layout, null); ImageView bannerImage = view.findViewById(R.id.banner_image); Glide.with(context).load(bannerMoviesList.get(position).getImageUrl()).into(bannerImage); container.addView(view); return view; } } 错误本身就告诉你哪里搞砸了。您的项目中有 2 个 CategoryItem 类,具有以下层次结构: com.kuroniczstudio.splashscreen.CategoryItem com.kuroniczstudio.splashscreen.model.CategoryItem 您试图同时使用这两种方法,从而造成了冲突。确保您只使用一个类。 在您的应用程序中,您有两个名为 CategoryItem 的类。 其中一个有全名: com.kuroniczstudio.splashscreen.CategoryItem 对方的全名: com.kuroniczstudio.splashscreen.model.CategoryItem 它们是不同的类别,您无法从一个类别转换为另一类别。 我们无法从代码中得知您向我们展示了为什么会发生这种情况。 首先,您还没有向我们展示 CategoryItem 的两个版本。 并且您遗漏了所有 package 语句。 (所以我们无法判断 CategoryItem 构造函数需要哪个版本的 AllCategory ...) 但是编译错误消息清楚地证明它正在发生。 你可能根本不应该上两门CategoryItem课程...... 免费下载最新版本的 Castle Mod APK,无广告且解锁所有功能。单击下载按钮即可开始观看电影、节目和现场板球比赛。 https://castleappk.org/
在PagerAdapter上调用notifyDataSetChanged后TabLayout滚动到未知位置
我有 TabLayout 和 PagerAdapter 的示例项目。 当我调用 pagerAdapter.notifyDataSetChanged(); 时,TabLayout 会发生奇怪的事情;在 tabLayout.setupWithViewPager(viewPager) 之后; TabLayo...
Android Studio 中 TableLayout 和视图寻呼机中的单击事件问题
我正在开发一个应用程序,我试图构建选项卡布局并在其中插入片段,但在启动时它闪烁,之后它既没有被单击也没有被滚动 问题...
我正在毛伊岛开发一个应用程序,我已经搜索了一堆文件,但我找不到如何更改导航栏区域的颜色。请帮助我,我不明白如何解决它......
我想在每个选项卡之间设置边距。就像在 PagerTabStrip 中一样,它有 setTextSpacing(int textSpacing) 来在选项卡之间设置文本间距。 TabLayout 可以吗?
我有一个 Activity,其中包含三个片段。我将 TabLayout 与寻呼机一起使用,以便我可以在片段之间滑动。在这些片段之一上将有一个回收器视图,其中很少......
Android Studio [JAVA] - 更改导航栏菜单时缺少某些选项卡布局片段
所以,我将 BottomNavbar 与 Tablayout 结合起来,一开始它工作正常,但是当我从底部导航栏从主页更改菜单然后再次返回菜单时,其中一个 tablayout 片段消失了 ...
Android:单击按钮时如何在片段中显示带有选项卡布局的窗口(如弹出窗口)
我有一个片段,我需要显示一个不覆盖整个屏幕的窗口,并且我需要在该弹出窗口中显示两个选项卡。 单击
我有一个用于两种不同类型的类别的选项卡布局:收入和费用。我使用 CategoriesViewModel 获取所有类别,并观察列表是否有任何更改。当选择一个选项卡时...
可能你们中的许多人(像我一样)在创建带有底部点的 ViewPager 时遇到问题,如下所示: 如何创建这样一个 Android ViewPager?
我目前正在努力向 XML 添加选项卡布局,但我不知道如何在长按选项卡时删除工具提示。有人知道吗? 这是它看起来像的图像 htt...
我在自定义视图中添加了相对布局,并在选项卡布局中添加了它。我对选项卡使用白色背景,并且没有在选项卡自定义布局中应用任何填充。但我也很...
我是编程新手,我想添加一个设置菜单按钮。 如图所示,我的应用程序不显示三点按钮。 MainActivity.java: 公共类 MainActivity 扩展了 AppCompatAct...
为什么在使用 Tablayout 和 viewpager 时选项卡在片段中不可见?
所以,我正在开发我的 android 项目,并且正在为我的社区页面(这是一个片段)开发 UI。我正在使用 Tablayout 和 viewpager。我已经完成了所有工作并设置了适配器...
在 Android 主屏幕小部件内实现选项卡布局,并仅在选项卡项上单击片段切换
我的目标是仅当用户点击选项卡项目时才应更改文本,并禁用垂直/水平滑动。 我的目标是仅当用户点击选项卡项目时才应更改文本,并禁用垂直/水平滑动。 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:id="@+id/tab1Container" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:id="@+id/tab1Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 1 Content" /> </RelativeLayout> <RelativeLayout android:id="@+id/tab2Container" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="8dp"> <TextView android:id="@+id/tab2Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 2 Content" /> </RelativeLayout> </LinearLayout> public class MyWidgetProvider extends AppWidgetProvider { @Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { for (int appWidgetId : appWidgetIds) { RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); setTabClickListener(context, views, appWidgetId, R.id.tab1Container, "Tab 1 Clicked"); setTabClickListener(context, views, appWidgetId, R.id.tab2Container, "Tab 2 Clicked"); appWidgetManager.updateAppWidget(appWidgetId, views); } } private void setTabClickListener(Context context, RemoteViews views, int appWidgetId, int containerId, String contentText) { Intent intent = new Intent(context, MyWidgetProvider.class); intent.setAction("TAB_CLICKED"); intent.putExtra("CONTENT_TEXT", contentText); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetId, intent, 0); views.setOnClickPendingIntent(containerId, pendingIntent); } @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); if ("TAB_CLICKED".equals(intent.getAction())) { String contentText = intent.getStringExtra("CONTENT_TEXT"); updateContent(context, contentText); } } private void updateContent(Context context, String contentText) { Toast.makeText(context, contentText, Toast.LENGTH_SHORT).show(); } } 我需要在android widget中实现一个类似开关的按钮,我只是想知道是否可以使用选项卡布局来实现该功能,如果不可能,请建议其他方法 只需尝试使用自定义选项卡布局并根据您的需求进行自定义即可。 Tablayout 提供了停止滚动和设置选项卡重力的方法。
这是我的代码: 私有 TabLayout tabLayout; 私有 int[] tabIcons = { R.mipmap.ic_compass, R.mipmap.ic_place, R.mipmap.ic_passport, R.mipmap.ic_setting }; ...
更改 Jetpack Compose 中选定的选项卡底线颜色
我正在使用 Kotlin for Android 开发 Jetpack Compose 项目,并且使用 ScrollableTabRow 实现了选项卡布局。 Tabs有底线,我想改变选中的t的颜色...
我正在使用选项卡布局,这是我的代码 我正在使用选项卡布局,这是我的代码 <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="center" app:tabMode="fixed" android:minHeight="?attr/actionBarSize" app:tabTextColor="#000" app:tabSelectedTextColor="#fff" app:tabIndicatorColor="@android:color/white" android:clipToPadding="false" /> 像这样添加标签 //创建选项卡 TextView tab = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tab.setText("主页"); tab.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_home_black_24dp, 0, 0); tabLayout.addTab(tabLayout.newTab().setCustomView(tab)); TextView tab2 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tab2.setText("Report"); tab2.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_trending_up_black_24dp, 0, 0); tabLayout.addTab(tabLayout.newTab().setCustomView(tab2)); TextView tab3 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tab3.setText("Medicine"); tab3.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_home_black_24dp, 0, 0); tabLayout.addTab(tabLayout.newTab().setCustomView(tab3)); TextView tab4 = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null); tab4.setText("More"); tab4.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.ic_trending_up_black_24dp, 0, 0); tabLayout.addTab(tabLayout.newTab().setCustomView(tab4)); 这是我的手机屏幕截图 https://i.stack.imgur.com/kYNs1.png 有两件事 1.如何删除选项卡布局左右两侧的空间? 2.如何更改活动和非活动选项卡的文本和图标颜色 从选项卡布局中的选项卡中删除填充 在 TabLayout 中,您必须将 tabPaddingEnd 和 tabPaddingStart 设置为 0dp。 <android.support.design.widget.TabLayout app:tabPaddingStart="0dp" app:tabPaddingEnd="0dp"/> 通过更改TabLayout所选选项卡的图标颜色 viewPager = (ViewPager) findViewById(R.id.viewpager); setupViewPager(viewPager); tabLayout = (TabLayout) findViewById(R.id.tabs); tabLayout.setupWithViewPager(viewPager); tabLayout.setOnTabSelectedListener( new TabLayout.ViewPagerOnTabSelectedListener(viewPager) { @Override public void onTabSelected(TabLayout.Tab tab) { super.onTabSelected(tab); int tabIconColor = ContextCompat.getColor(context, R.color.tabSelectedIconColor); tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); } @Override public void onTabUnselected(TabLayout.Tab tab) { super.onTabUnselected(tab); int tabIconColor = ContextCompat.getColor(context, R.color.tabUnselectedIconColor); tab.getIcon().setColorFilter(tabIconColor, PorterDuff.Mode.SRC_IN); } @Override public void onTabReselected(TabLayout.Tab tab) { super.onTabReselected(tab); } } ); 试试这个, <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@drawable/bg_forum_tab" app:tabIndicatorColor="@color/colorBtnBg" app:tabIndicatorHeight="0dp" app:tabPaddingBottom="-1dp" app:tabPaddingEnd="-1dp" app:tabPaddingStart="-1dp" app:tabPaddingTop="-1dp" style="@style/CustomTabLayoutStyle" app:tabTextColor="@color/colorWhite" /> 像这样设置 tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom Styles: <style name="CustomTabLayoutStyle" parent="Base.Widget.Design.TabLayout"> <item name="tabSelectedTextColor">@color/tab_text_selected</item> <item name="tabIndicatorColor">@color/tab_indicator</item> <item name="tabTextAppearance">@style/CustomTabTexStyle</item> </style> <style name="CustomTabTexStyle" parent="TextAppearance.Design.Tab"> <item name="android:textSize">14sp</item> <item name="android:textColor">@color/tab_text</item> ... </style> 要调整填充,您可以使用 app:tabMinWidth 或 tabPaddingStart/tabPaddingEnd/tabPaddingTop/tabPaddingBottom <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="center" app:tabMode="fixed" app:tabMinWidth="50dp" app:tabPaddingBottom="1dp" app:tabPaddingEnd="1dp" app:tabPaddingStart="1dp" app:tabPaddingTop="1dp" android:minHeight="?attr/actionBarSize" app:tabTextColor="#000" app:tabSelectedTextColor="#fff" app:tabIndicatorColor="@android:color/white" android:clipToPadding="false" /> 简单来说,您可以使用下面的颜色代码 <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@color/tab_background_selected" android:state_selected="true"/> <item android:drawable="@color/tab_background_unselected"/> </selector> 并将其保存在可绘制对象中为 tab_color_selector.xml 并使用 app:tabBackground="@drawable/tab_color_selector" 所以总代码是 <android.support.design.widget.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="center" app:tabMode="fixed" app:tabMinWidth="50dp" android:minHeight="?attr/actionBarSize" app:tabTextColor="#000" app:tabSelectedTextColor="#fff" app:tabIndicatorColor="@android:color/white" app:tabBackground="@drawable/tab_color_selector" android:clipToPadding="false" /> 在 tab_background_selected 和 tab_background_unselected 中提供颜色,如 #000000。或者直接使用 #000000 代替 @color/tab_background_unselected 您可以添加属性: app:tabIndicatorFullWidth="false" app:tabIndicatorHeight="0dp" app:tabMinWidth="0dp" app:tabPadding="0dp" app:tabPaddingBottom="0dp" app:tabPaddingEnd="0dp" app:tabPaddingStart="0dp" app:tabPaddingTop="0dp"