首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin:风格TabWidget

Xamarin:风格TabWidget
EN

Stack Overflow用户
提问于 2013-12-17 07:11:53
回答 2查看 4.4K关注 0票数 0

我正在使用TabHost作为android中的选项卡布局。我该如何设计标签的样式?

如图所示,选定的选项卡应向上膨胀。如何将边框设置为选项卡?还有黑影?

每个选项卡的Xml,

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/ic_tab_speakers_selected" 
      android:state_selected="true"/>
  <item android:drawable="@drawable/ic_tab_speakers_unselected"/>
</selector>

通过使用以下代码,我成功地设置了背景色:

代码语言:javascript
复制
TabWidget.GetChildAt(0).SetBackgroundColor(Android.Graphics.Color.ParseColor("#8ACAE1"));

下面是我用来创建tabWidget的代码,

代码语言:javascript
复制
TabHost.TabSpec spec;            
TabHost.SetBackgroundColor(Android.Graphics.Color.ParseColor("#4B4B4B"));

var intent = new Intent(this, activityType);
intent.AddFlags(ActivityFlags.NewTask);

spec = TabHost.NewTabSpec(tag);

var drawableIcon = Resources.GetDrawable(drawableId);
spec.SetIndicator("", drawableIcon);

spec.SetContent(intent);
TabHost.AddTab(spec);

帮帮忙,谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-03 11:07:31

开始起作用了。

方法来创建选项卡。

代码语言:javascript
复制
    private void CreateTab(Type activityType, string tag, string colorcode, int viewId)
{
    TabHost.TabSpec spec;

    var intent = new Intent(this, activityType);
    intent.AddFlags(ActivityFlags.NewTask);

    spec = TabHost.NewTabSpec(tag);
    View tab = LayoutInflater.Inflate (viewId, null); // viewId = Resource.Layout.TabStyle

    spec.SetIndicator (tab);
    spec.SetContent(intent);

    TabHost.AddTab(spec);
} 

TabStyle.axml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabLayout"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:background="@drawable/ic_tab_state"
    android:layout_height="fill_parent">
    <ImageView
        android:src="@drawable/feed"
        android:layout_width="100dp"
        android:layout_height="fill_parent"
        android:scaleType="centerInside"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="9dp"
        android:layout_marginRight="9dp"
        android:layout_marginBottom="9dp"
        android:adjustViewBounds="true"
        android:layout_gravity="center_vertical|center_horizontal"
        android:id="@+id/tabIcon" />
</LinearLayout>

ic_tab_state.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/selectedTab"
          android:state_selected="true"/>
    <item android:drawable="@drawable/unselectedTab"/>
</selector>

selectedTab.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="6dp">
        <shape android:shape="rectangle">
            <gradient android:startColor="#AAAAAA" android:centerColor="#888888"
                android:endColor="#666666" android:angle="90" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="10dp"
                android:topRightRadius="10dp" />
        </shape>
    </item>
    <item android:top="4dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#8ACAE1" />
            <corners android:bottomRightRadius="0dp"
                android:bottomLeftRadius="0dp" android:topLeftRadius="8dp"
                android:topRightRadius="8dp" />
        </shape>
    </item>
</layer-list>

unselectedTab.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="11dp">
        <shape android:shape="rectangle">
            <solid android:color="#333333" />
        </shape>
    </item>
    <item android:top="12dp" android:left="1dp" android:right="1dp"
        android:bottom="0dp">
        <shape android:shape="rectangle">
            <solid android:color="#8ACAE1" />
        </shape>
    </item>
</layer-list>
票数 0
EN

Stack Overflow用户

发布于 2013-12-17 07:49:09

代码语言:javascript
复制
Use this code:

Button _button = new Button(context);
_button.setBackgroundResource("selector xml file");

TabSpec setContent = tabHost.newTabSpec(tag).setIndicator(_button)
            .setContent(new TabContentFactory() {
                public View createTabContent(String tag) {
                    return _button;
                }
            });

tabHost.addTab(setContent, class name, null);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20628047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档