我正在玩设计支持库TabLayout.我的问题是其中一个标签的标题太长,因此,它是在2行而不是1上绘制的.我想知道是否有一种方法可以缩放标题文本大小以确保所有标题都在1行上绘制.
这是一个截图,以更好地解释我的问题:
如果细节很重要,我正在使用Design Support TabLayout,一个ViewPager和一个FragmentPagerAdapter来填充我的标签.
提前致谢!
android fragmentpageradapter androiddesignsupport android-tablayout
在选项卡自定义布局中,我将其父元素match_parent设置为并设置其背景颜色.当我运行它时,标签显示自定义布局包装元素imageview和textview.我希望这个自定义布局将填充选项卡,选项卡之间没有任何空格.检查输出:
private void setupTabLayout(ViewPager viewPager, ViewPagerAdapter viewPagerAdapter) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
int length = tabLayout.getTabCount();
for (int i = 0; i < length; i++) {
tabLayout.getTabAt(i).setCustomView(viewPagerAdapter.getTabView(i));
}
}
Run Code Online (Sandbox Code Playgroud)
tab_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout"
android:background="@color/grey_accent">
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/icon"
android:src="@drawable/ic_action_home"
android:layout_marginBottom="19dp"
android:layout_above="@+id/title"
android:layout_centerHorizontal="true" />
<TextView
android:layout_gravity="center"
android:textColor="@color/white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home"
android:id="@+id/title"
android:layout_marginBottom="259dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
ViewPagerAdapter:
public View getTabView(int position) {
View view = LayoutInflater.from(this.context).inflate(R.layout.tab_layout, null);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView icon …Run Code Online (Sandbox Code Playgroud) android android-layout android-design-library androiddesignsupport android-tablayout
我正在使用带有3个选项卡的TabLayout.我自定义了我的标签视图,因此,我需要在我的标签下删除以下行(截图不是来自我的应用):
我没有使用TabHost或TabWidget,因此我无法使用setStripEnabled(false).将背景设置为透明也不会改变任何东西.
这是我的xml:
<android.support.design.widget.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:padding="4dip"
android:layout_above="@+id/bottomcontent3"
android:gravity="center_horizontal"
android:background="@android:color/white"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/comtabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:tabPaddingStart="0dp"
app:tabPaddingEnd="0dp"
app:tabMode="fixed"
app:tabGravity="fill"
android:background="@android:color/white" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/compager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
我找到的所有答案都在使用TabHost,TabWidget.就我而言,我正在使用一个TabLayout和三个Tab.在这种情况下如何删除此行?非常感谢.
编辑由于某些原因,我的代码无法解析TabLayout中的某些方法.有我使用的java代码:
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.comtabs);
tabLayout.setTabMode(TabLayout.MODE_FIXED);
// add tabs
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
tabLayout.addTab(tabLayout.newTab());
RelativeLayout layout1 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutleft, container, false);
RelativeLayout layout2 = (RelativeLayout) inflater.inflate(R.layout.communitytablayout, container, false);
RelativeLayout layout3 = (RelativeLayout) inflater.inflate(R.layout.communitytablayoutright, container, false);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL); …Run Code Online (Sandbox Code Playgroud) 我正在一个包含三个选项卡的屏幕上工作我试图在选项卡中添加一个带有我的文本的图标,我希望图像在文本的上方,它们之间应该有一些空间,这是我的代码.
public class HomeScreen extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private Toolbar toolbar;
private ViewPager pager;
private ViewPagerAdapter adapter;
private SlidingTabLayout tabs;
private CharSequence Titles[] = {"News", "Most Views", "Chart"};
int Numboftabs = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//MAhmoud Code Addtion
// getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// getSupportActionBar().setIcon(R.drawable.ic_launcher);
// Creating The …Run Code Online (Sandbox Code Playgroud) android android-layout android-tabs material-design android-tablayout
我的活动使用TabLayout+ ViewPager.
此处的选项卡和页面数量是动态的,具体取决于从服务器获取的数据.
崩溃是通过Crashlytics报道的,我无法复制它.
我的活动代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
boolean isAppRestarting = PrefUtils.getBoolean("app_restarting", false);
if (isAppRestarting) {
super.onCreate(null);
this.savedInstanceState = null;
} else {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstanceState;
}
initSlidingTabs();
fetchSomeData(); // see onDataFetched() below
}
private void initSlidingTabs() {
List<FragmentWithTitle> sdsTabs = new ArrayList<>();
Logger.i("There is saved instance state");
ArrayList<String> titles = savedInstanceState.getStringArrayList(TAB_TITLES);
if (titles != null) {
for (int i = 0; i < titles.size(); i++) {
if (getFragment(i) != null) {
sdsTabs.add(new …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager fragmentpageradapter android-tablayout
我想在每个标签之间设置保证金.就像在PagerTabStrip中有setTextSpacing(int textSpacing)来制作tab之间的文本间距一样.TabLayout能做到吗?
android android-layout android-support-library androiddesignsupport android-tablayout
我有这个代码用于更改标签布局文本的颜色,但它根本不起作用!
app:tabTextColor 不起作用,它不能改变颜色为白色.
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:tabIndicatorColor="@color/blue"
app:tabIndicatorHeight="5dp"
app:tabTextColor="@color/white" />
Run Code Online (Sandbox Code Playgroud) TabLayout我的应用程序中有两种不同的样式:
<style name="TabLayoutPower" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">@android:color/white</item>
<item name="tabTextColor">@android:color/white</item>
</style>
<style name="TabLayoutFree" parent="Widget.Design.TabLayout">
<item name="tabSelectedTextColor">@android:color/black</item>
<item name="tabTextColor">@android:color/black</item>
</style>
Run Code Online (Sandbox Code Playgroud)
如何定义TabLayout主题的默认样式?我找不到任何信息,我应该使用哪个项目名来构建我的主题.我想像添加listview一样添加TabLayout:
<style name="Free" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/black</item>
<item name="colorPrimaryDark">@color/black</item>
<item name="colorAccent">@color/main_red</item>
<item name="android:windowBackground">@color/main_bg_light</item>
<item name="android:listViewStyle">@style/MyListView</item>
</style>
Run Code Online (Sandbox Code Playgroud) 我在AppBarLayout下面有一个ViewPager(带有工具栏和TabLayout).我无法理解为什么加载的片段的高度超过可用空间,即使没有这么大的元素,使标签可滚动.
这是主要的布局xml:
<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:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="fill"
app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
正如你在我的片段中看到的那样,我有一个带有layout_alignParentBottom的Button,但我猜它会拉伸viewpager的高度太多.
这里是设计视图的布局:
我正在开发一个具有标签布局作为图像的应用程序.
我想将MVVM架构与数据绑定库一起使用,但我是这个框架的新手.
我可以通过使用ViewPager作为此示例的常规设置选项卡布局而不使用MVVM来执行此操作.
没有MVVM和数据绑定的常规选项卡布局:
activity_main.xml中:
<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: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="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Run Code Online (Sandbox Code Playgroud)
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
private void setupViewPager(ViewPager viewPager) …Run Code Online (Sandbox Code Playgroud) data-binding android mvvm android-tablayout android-databinding