fin*_*nki 40 xml layout tabs android android-support-library
我刚刚将新的TabLayout组件添加到我的应用程序中.正如你可能知道有针对标签两种不同的模式app:tabMode="scrollable"
和app:tabMode="fixed"
.
当我使用时,app:tabMode="fixed"
我得到以下结果:
左侧和右侧没有边距/填充,但文本被包裹.
但是当我使用时,app:tabMode="scrollable"
我得到以下结果:
文字没有被包裹,但这里是一个奇怪的边缘,我无法摆脱它.
我也试过tabGravity设置要么app:tabGravity="center"
或app:tabGravity="fill"
但未取得任何变化.
如果你们这些聪明的家伙和女孩能为我找到解决方案,那就太好了.
干杯,卢卡斯
Tal*_*tle 32
这是一个快速破解,比使用更短setCustomView()
:使用android:theme
你的属性TabLayout
:
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/TabLayout_Theme"
app:tabMode="fixed"/>
Run Code Online (Sandbox Code Playgroud)
然后在您的主题XML中:
<style name="TabLayout_Theme" parent="@style/AppTheme">
<item name="android:singleLine">true</item>
</style>
Run Code Online (Sandbox Code Playgroud)
我们必须这样做,因为遗憾的android:singleLine
是app:tabTextAppearance
在TabLayout 上的集合上忽略了该属性.app:tabTextAppearance
实际上只对更改文本大小有用.
Dan*_*ent 28
这里的一个解决方案是为每个选项卡扩展自定义布局,这将使您可以更好地控制每个选项卡的外观.这是通过setCustomView()方法完成的.
请注意,它在不同的屏幕分辨率上会有所不同.
在每台设备上使它看起来很完美总是很难,但至少使用这种方法可以提供更多控制,因为您可以使用不同的自定义布局xml文件来处理不同的屏幕分辨率/尺寸.
一种方法是使字体大小尽可能大,而不会在每个屏幕尺寸上切断.
我有一个简单的示例工作,它将每个选项卡中的文本限制为一行,但是在这个简单的示例中,它还会导致侧面选项卡中的长文本进行椭圆化而不更改字体大小.下一步是找出每个屏幕大小的最佳字体大小,并为每个屏幕大小创建一个特定的选项卡布局xml.
以下是custom_tab.xml文件,其中android:singleLine="true"
指定了:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/custom_text"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:textSize="16dip"
android:textColor="#ffffff"
android:singleLine="true"
/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是MainActivity的布局:
<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=".MainActivity">
<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"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
app:tabMode="fixed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/toolbar"
android:background="?attr/colorPrimary"
android:elevation="6dp"
app:tabTextColor="#d3d3d3"
app:tabSelectedTextColor="#ffffff"
app:tabIndicatorColor="#ff00ff"
android:minHeight="?attr/actionBarSize"
/>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_below="@id/tab_layout"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
这是Activity代码,其中包含FragmentPagerAdapter:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Get the ViewPager and set it's PagerAdapter so that it can display items
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
PagerAdapter pagerAdapter =
new PagerAdapter(getSupportFragmentManager(), MainActivity.this);
viewPager.setAdapter(pagerAdapter);
// Give the TabLayout the ViewPager
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
// Iterate over all tabs and set the custom view
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
tab.setCustomView(pagerAdapter.getTabView(i));
}
}
class PagerAdapter extends FragmentPagerAdapter {
String tabTitles[] = new String[] { "Aufzeichnung", "Berichte", "Neue Aufgabe", };
Context context;
public PagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return tabTitles.length;
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new BlankFragment();
case 1:
return new BlankFragment();
case 2:
return new BlankFragment();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
public View getTabView(int position) {
View tab = LayoutInflater.from(MainActivity.this).inflate(R.layout.custom_tab, null);
TextView tv = (TextView) tab.findViewById(R.id.custom_text);
tv.setText(tabTitles[position]);
return tab;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是上面代码的结果:
请注意,如果您删除android:singleLine="true"
,它看起来像这样,与您在问题中的外观类似:
Hit*_*ahu 15
在标题标题中显示"..."将无法获得良好的UI体验我建议您将tabmode设置为可滚动,并让标题标题占用他们想要的空间.
<android.support.design.widget.TabLayout
android:id="@+id/htab_tabs"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable" />
Run Code Online (Sandbox Code Playgroud)
如果这是一个选项,您可以使用 RelativeLayout 并通过设置 android:layout_centerHorizontal="true" 将选项卡居中
这将使您在左侧和右侧具有相等的边距。
例如
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:tabMode="scrollable" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19911 次 |
最近记录: |