Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何增加选项卡项上图标的大小?_Java_Android_Android Drawable_Android Tablayout_Android Icons - Fatal编程技术网

Java 如何增加选项卡项上图标的大小?

Java 如何增加选项卡项上图标的大小?,java,android,android-drawable,android-tablayout,android-icons,Java,Android,Android Drawable,Android Tablayout,Android Icons,我的项目在tabLayout上包括6个图标。它们都以编程方式放入选项卡,而不是使用layout中的ImageView。现在,必须增加平板电脑屏幕图标的大小 我尝试了很多东西;已尝试增加选项卡项的图标大小、宽度和高度。但这些都没用 我如何增加tabLayout上的图标大小?希望有人能帮我 Result_Activity.java private Integer[] images; Integer[] domesticImages = { R.drawable.ic_directio

我的项目在
tabLayout
上包括6个图标。它们都以编程方式放入
选项卡,而不是使用
layout
中的
ImageView
。现在,必须增加平板电脑屏幕图标的大小

我尝试了很多东西;已尝试增加选项卡项的图标大小、宽度和高度。但这些都没用

我如何增加tabLayout上的图标大小?希望有人能帮我

Result_Activity.java

private Integer[] images;
Integer[] domesticImages = {
        R.drawable.ic_directions_bus_black_24dp,
        R.drawable.ic_flight_takeoff_black_24dp,
        R.drawable.ic_directions_boat_black_24dp,
        R.drawable.ic_train_black_24dp,
        R.drawable.ic_hotel,
        R.drawable.ic_rent_a_car
};
private Integer[] domesticImagesClicked = {
        R.drawable.ic_directions_bus_clicked_24dp,
        R.drawable.ic_flight_takeoff_clicked_24dp,
        R.drawable.ic_directions_boat_clicked_24dp,
        R.drawable.ic_train_clicked_24dp,
        R.drawable.ic_hotel_clicked,
        R.drawable.ic_rent_a_car_clicked
};
activity_result.xml

 <android.support.design.widget.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:layout_below="@+id/rl_Detail"
            android:background="#F4E6CA"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:tabBackground="?attr/selectableItemBackgroundBorderless"
            app:tabIndicatorColor="@android:color/transparent"
            app:tabPaddingEnd="15dp"
            app:tabPaddingStart="15dp">

            <android.support.design.widget.TabItem
                android:id="@+id/tab1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:saveEnabled="true" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="match_parent" />

            <android.support.design.widget.TabItem
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

        </android.support.design.widget.TabLayout>

您可以为平板电脑配置指定不同的资源集(可绘制文件夹)

平板电脑需要另一个名为drawable-sw600dp的可绘图文件夹

如和中定义的

另一种方法是为选项卡创建自定义视图

您可以为平板电脑的“自定义”选项卡创建布局

然后以编程方式对布局进行充气,并使用
tab.setCustomView(customView)
进行设置


您只能在平板电脑的情况下执行此操作。

您可以为平板电脑配置指定不同的资源集(可绘制文件夹)

平板电脑需要另一个名为drawable-sw600dp的可绘图文件夹

如和中定义的

另一种方法是为选项卡创建自定义视图

您可以为平板电脑的“自定义”选项卡创建布局

然后以编程方式对布局进行充气,并使用
tab.setCustomView(customView)
进行设置


您只能在平板电脑上执行此操作。

如果您希望不同屏幕尺寸的图标大小不同,您可能需要选择以下任一选项:

1-制作大小合适的不同图像并放入相应的文件夹中,因为android为每个dpi密度提供了可绘制的文件夹,请查看:

2-如果图标很简单,您可以使用矢量图标,因为它们可以更改大小以适合您需要的任何屏幕大小。看看:

3-您可以检查设备是否为平板电脑,并通过编程更改图标大小,这取决于您添加图标的方式。您可以使用以下代码检查:

publicstaticbooleanistablet(上下文){
返回(context.getResources().getConfiguration().screenLayout&0xF)>=3;
}

或者,您可以在此处查看不同的方法:

如果这些方法都没有帮助,那么有一个官方的详细指南:

如果您希望不同屏幕尺寸的图标大小不同,您可能需要选择以下任何一种:

1-制作大小合适的不同图像并放入相应的文件夹中,因为android为每个dpi密度提供了可绘制的文件夹,请查看:

2-如果图标很简单,您可以使用矢量图标,因为它们可以更改大小以适合您需要的任何屏幕大小。看看:

3-您可以检查设备是否为平板电脑,并通过编程更改图标大小,这取决于您添加图标的方式。您可以使用以下代码检查:

publicstaticbooleanistablet(上下文){
返回(context.getResources().getConfiguration().screenLayout&0xF)>=3;
}

或者,您可以在此处查看不同的方法:

如果这些方法都没有帮助,那么有一个官方的详细指南:

您可以使用

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        switch(i){
            case 0: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_1)
                        .setText(R.string.your_text1);
                    }break;
            case 1: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_2)
                        .setText(R.string.your_text2);
                    }break;
            case 2: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_3)
                        .setText(R.string.your_text3);
                    }break;
            case 3: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_4)
                        .setText(R.string.your_text4);
                    }break;
        }
        if (tab != null) tab.setCustomView(R.layout.your_custom_view);
    }
for(int i=0;i
创建一个新的布局资源作为您的_custom _view.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@android:id/icon"
            android:layout_width="@dimen/tab_icon_size"
            android:layout_height="@dimen/tab_icon_size"
            android:layout_centerInParent="true"/>
    </RelativeLayout>

您可以使用

    for (int i = 0; i < tabLayout.getTabCount(); i++) {
        TabLayout.Tab tab = tabLayout.getTabAt(i);
        switch(i){
            case 0: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_1)
                        .setText(R.string.your_text1);
                    }break;
            case 1: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_2)
                        .setText(R.string.your_text2);
                    }break;
            case 2: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_3)
                        .setText(R.string.your_text3);
                    }break;
            case 3: if (tab != null){
                        tab.setIcon(R.drawable.your_icon_4)
                        .setText(R.string.your_text4);
                    }break;
        }
        if (tab != null) tab.setCustomView(R.layout.your_custom_view);
    }
for(int i=0;i
创建一个新的布局资源作为您的_custom _view.xml,如下所示

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@android:id/icon"
            android:layout_width="@dimen/tab_icon_size"
            android:layout_height="@dimen/tab_icon_size"
            android:layout_centerInParent="true"/>
    </RelativeLayout>


如何添加图标?你能把密码寄出去吗?如果以编程方式添加图标,则在添加图标之前,可能还需要以编程方式更改其大小。@Ric