java - 如何设置平板电脑的 tablayout 图标大小
问题描述
如何为平板电脑设置标签图标大小我做了很多研究,尝试了很多方法,但无法使其适应我的布局。手机屏幕正常,但平板电脑屏幕上的“7”“10”图标尺寸仍然很小。
public class MainActivity extends AppCompatActivity {
private TabLayout tabLayout;
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager) findViewById(R.id.viewpager);
setupViewPager(viewPager);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
setupTabIcons();
我以编程方式获取图标。
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabOne.setText(R.string.Scanner);
tabOne.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_image, 0, 0);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabTwo.setText(R.string.Create);
tabTwo.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.create, 0, 0);
tabLayout.getTabAt(1).setCustomView(tabTwo);
TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabThree.setText(R.string.History);
tabThree.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_history, 0, 0);
tabLayout.getTabAt(2).setCustomView(tabThree);
TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabFour.setText(R.string.Settings);
tabFour.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.ic_settings, 0, 0);
tabLayout.getTabAt(3).setCustomView(tabFour);
}
}
我的活动主布局
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="@dimen/_45sdp"
app:tabGravity="fill"
app:tabMode="fixed" />
</com.google.android.material.appbar.AppBarLayout>
custom_tab 布局
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif"
android:gravity="center_horizontal"
android:textColor="@color/custom_tabtext1"
android:textSize="@dimen/_9ssp"
/>
解决方案
我认为您可以使用 3 种方式支持平板电脑。
为平板电脑支持创建 3 个布局文件的第一种方法:-
res/layout/main_activity.xml # For handsets (smaller than 600dp)
res/layout-sw600dp/main_activity.xml # For 7” tablets (600dp wide and bigger)
res/layout-sw720dp/main_activity.xml # For 10” tablets (720dp wide and bigger)
为平板电脑支持创建 3 维文件的第二种方法:-
values/dimens.xml # For handsets (smaller than 600dp)
values-sw600dp/dimens.xml # For 7” tablets (600dp wide and bigger)
values-sw720dp/dimens.xml # For 10” tablets (720dp wide and bigger)
使用平板电脑支持的“Sdp”库的第三种方法:-
推荐阅读
- python - 将具有多列的熊猫数据框转置为多行
- reactjs - AsyncStorage 将令牌传递给标头发布请求
- php - Codeignitor URL 冲突
- c - 动态 DMA 传输
- python - Mask-RCNN/TensorFlow:不同的图像格式(jpg、png)会影响Mask-RCNN的训练结果吗?
- python - 选择熊猫中的行,其中一列中的值是另一列中值的子字符串
- html - 在两个 div 之间定位一个 div
- visual-studio-code - VSCode 扩展 API - 我们可以在 Visual Studio 代码中添加条件上下文菜单吗?
- sql - SQL Server 存储过程重写
- javascript - 更改图像的格式名称