如何在Android中创建圆角选项卡?

时间:2016-12-07 06:01:26

标签: android

有没有人知道如何创建这样的圆角标签:

预期标签: enter image description here

即将到来的标签: enter image description here 希望你能告诉我。谢谢。

3 个答案:

答案 0 :(得分:0)

@Vinodh我在ViewPager中使用Tablayout。我将一个样式设置为Tablayout,并使用如下选择器设置tabBackground属性:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true">
    <shape>
        <corners android:topLeftRadius="8dp" android:topRightRadius="8dp"/>
        <solid android:color="#fff"/>
    </shape>
</item>

<item>
    <layer-list>
        <item>
            <shape>
                <corners android:topLeftRadius="8dp" android:topRightRadius="8dp"/>
                <solid android:color="@color/white"/>
            </shape>
        </item>
        <item>
            <shape>
                <corners android:radius="8dp"/>
                <solid android:color="#000"/>
            </shape>
        </item>

    </layer-list>


</item>

并设置Tablayout的背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

<corners android:topLeftRadius="8dp" android:topRightRadius="8dp"/>
<solid android:color="#000"/>

答案 1 :(得分:0)

尝试下面的形状

T.U

答案 2 :(得分:0)

如果要更改选项卡布局,请在可绘制中自定义背景,并在选中和选中的选项卡中设置圆角

first tab selected

saved tab selected

在您的活动/片段中 我在 tablayout 中设置了背景,所以你可以根据你的要求制作背景

<块引用>

为简洁起见仅添加自定义代码

import org.springframework.boot.autoconfigure.web.WebProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.CacheControl;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.concurrent.TimeUnit;

@Configuration
public class CacheStaticResourcesConfiguration implements WebMvcConfigurer {

    /**
     * We provide a custom configuration which resolves URL-Requests to static files in the
     * classpath (src/main/resources directory).
     *
     * This overloads a default configuration retrieved at least partly from
     * {@link WebProperties.Resources#getStaticLocations()}.
     *
     * @param registry ResourceHandlerRegistry
     */
    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        /*
         * BE AWARE HERE:
         *
         * .addResourceHandler(): URL Paths
         * .addResourceLocations(): Paths in Classpath to look for file
         *   root "/" refers to src/main/resources
         *   For configuration example, see:
         *     org.springframework.boot.autoconfigure.web.WebProperties.Resources().getStaticLocations()
         *
         * .addResourceLocations("classpath:/static/")
         *   =>
         *      addResourceHandler("/**")
         *      => GET /res/css/main.css
         *         => resolved as: "classpath:/static/res/css/main.css"
         *      BUT
         *      addResourceHandler("/res/**")
         *      => GET /res/css/main.css
         *            (spring only appends the ** to the value from
         *             addResourceLocations())
         *         => resolved as: "classpath:/static/css/main.css"
         */

        registry
                .addResourceHandler("/favicon.ico")
                // trailing slash is important!
                .addResourceLocations("classpath:/static/")
                .setCacheControl(CacheControl.maxAge(1, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());

        registry
                .addResourceHandler("/res/**")
                // trailing slash is important!
                .addResourceLocations("classpath:/static/res/")
                .setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());

        registry
                .addResourceHandler("/images/**")
                // trailing slash is important!
                .addResourceLocations("classpath:/static/images/")
                .setCacheControl(CacheControl.maxAge(7, TimeUnit.DAYS)
                        .noTransform()
                        .mustRevalidate());
    }
}

在 Drawable 中

1.tab_bg_normal

val tabCount: Int = images_videos_tab_layout.tabCount

    for (i in 0 until tabCount) {
        val tabView: View = (images_videos_tab_layout.getChildAt(0) as ViewGroup).getChildAt(i)
        tabView.requestLayout()
        ViewCompat.setBackground(tabView,setImageButtonStateNew(requireContext()));
        ViewCompat.setPaddingRelative(tabView, tabView.paddingStart, tabView.paddingTop, tabView.paddingEnd, tabView.paddingBottom);
    }

fun setImageButtonStateNew(mContext: Context): StateListDrawable {
    val states = StateListDrawable()
    states.addState(intArrayOf(android.R.attr.state_selected), ContextCompat.getDrawable(mContext, R.drawable.tab_bg_normal_blue))
    states.addState(intArrayOf(-android.R.attr.state_selected), ContextCompat.getDrawable(mContext, R.drawable.tab_bg_normal))

    return states
}

2.tab_bg_normal_blue

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/your_unselected_tab_color" />
<corners android:radius="25dp" />
</shape>

3.tabview_bg

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/baseThemeColor" />
<corners android:radius="25dp" />
</shape>