如何在android中制作椭圆形tabhost

如何在android中制作椭圆形tabhost,第1张

概述我试着按照预期的形状制作椭圆形tabhost. 我尝试了以下代码. public class AndroidTabLayoutActivity extends TabActivity { TabHost tabHost; @Override public void onCreate(Bundle savedInstanceState) { super.on 我试着按照预期的形状制作椭圆形tabhost.

我尝试了以下代码.

public class AndroIDTabLayoutActivity extends tabactivity {    TabHost tabHost;    @OverrIDe    public voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.main);        tabHost = getTabHost();        TabHost.TabSpec spec;        Intent intent;        intent = new Intent().setClass(this,PhotosActivity.class);        VIEw tabVIEw = createTabVIEw(this,"Updates");        spec = tabHost.newTabSpec("tab1").setIndicator(tabVIEw).setContent(intent);        tabHost.addTab(spec);        tabVIEw = createTabVIEw(this,"Events");        intent = new Intent().setClass(this,SongsActivity.class);        spec = tabHost.newTabSpec("tab2").setIndicator(tabVIEw)                .setContent(intent);        tabHost.addTab(spec);        TabWidget tabWidget = (TabWidget) findVIEwByID(androID.R.ID.tabs);        final int tabChildrenCount = tabWidget.getChildCount();        VIEw currentVIEw;        for (int i = 0; i < tabChildrenCount; i++) {            currentVIEw = tabWidget.getChildAt(0);            linearLayout.LayoutParams currentLayout =                    (linearLayout.LayoutParams) currentVIEw.getLayoutParams();            currentLayout.setmargins(0,16,0);        }        tabWidget.requestLayout();        tabHost.getTabWidget().setdivIDerDrawable(null);    }    private static VIEw createTabVIEw(Context context,String tabText) {        VIEw vIEw = LayoutInflater.from(context).inflate(R.layout.custom_tab,null,false);        TextVIEw tv = (TextVIEw) vIEw.findVIEwByID(R.ID.tabTitleText);        tv.setText(tabText);        return vIEw;    }}

main.xml中

<?xml version="1.0" enCoding="utf-8"?><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:layout_wIDth="fill_parent"androID:layout_height="fill_parent"androID:background="#2CA0E6"><linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="fill_parent"    androID:layout_height="fill_parent"    androID:layout_marginBottom="20dp"    androID:layout_margintop="30dp"    androID:paddingleft="20dp"    androID:paddingRight="20dp">    <TabHost        androID:ID="@androID:ID/tabhost"        androID:layout_wIDth="fill_parent"        androID:layout_height="fill_parent"        androID:layout_margintop="2dp">        <linearLayout            androID:layout_wIDth="fill_parent"            androID:layout_height="fill_parent"            androID:orIEntation="vertical"            androID:paddingtop="5dp">            <TabWidget                androID:ID="@androID:ID/tabs"                androID:layout_wIDth="fill_parent"                androID:layout_height="70dp" />            <FrameLayout                androID:ID="@androID:ID/tabcontent"                androID:layout_wIDth="fill_parent"                androID:layout_height="fill_parent" />        </linearLayout>    </TabHost></linearLayout>

custom_tab.xml

<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:ID="@+ID/tabTitleText"androID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:gravity="center"androID:clickable="true"androID:padding="5dp"androID:textSize="15sp"androID:textStyle="bold"androID:layout_gravity="center"androID:ellipsize="marquee"androID:singleline="true"androID:textcolor="@color/tab_textcolor"androID:background="@drawable/tab_selector"/>

我得到的输出如下image70001

任何人都可以帮助,如何做到这一点.谢谢

解决方法 那是一个图像.你需要做的就是准备好图像并将它们设置到选定的标签.就是这样!

好吧,我没有那个图像,所以我使用下面的图像(selected.png,not_selected.png)只是为了说明它是如何工作的,但它们设计得不好;)

P.surrentLayout.setmargins(0,this_should_be_zero,0);并且您的图像应该具有该边距(无论预期的间隙),否则两个图像之间将存在间隙.
此外,您可以使用选择器(与另一种颜色相同的png)来显示所选的一个.

似乎你试图找出一种编程方式,如果你有额外的时间和时间,可以尝试使用Paint类进行解决方法.努力,如果你使用形状将很难弄清楚确切的视图,因为它很复杂,你可以看到标签A视图和B不相同,使用图像将是最简单的

并在您的custom_tab.xml集中

<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:ID="@+ID/tabTitleText"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:gravity="center"    androID:clickable="true"    androID:padding="5dp"    androID:textSize="15sp"    androID:textStyle="bold"    androID:layout_gravity="center"    androID:ellipsize="marquee"    androID:Maxlines="1"   //  you can use this instead  of  androID:singleline="true"     androID:textcolor="@color/black"    androID:background="@drawable/tab_button_effect"/> // here set selector

tab_button_effect.xml

<?xml version="1.0" enCoding="utf-8"?><selector xmlns:androID="http://schemas.androID.com/apk/res/androID">    <item androID:drawable="@drawable/not_selected" androID:state_selected="true"></item>    <item androID:drawable="@drawable/selected"></item></selector>
总结

以上是内存溢出为你收集整理的如何在android中制作椭圆形tabhost全部内容,希望文章能够帮你解决如何在android中制作椭圆形tabhost所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1136271.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存