【问题标题】:How to create curved or rounded tabs in Android如何在 Android 中创建弯曲或圆形标签
【发布时间】:2020-06-20 18:02:58
【问题描述】:

我想在标签的右下方显示曲线。那么怎么做呢?下面是我用于 XML 和 Java 文件的代码。

第一个 XML 的代码

<?xml version="1.0" encoding="utf-8"?>

<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TabWidget
    android:id="@android:id/tabs"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"/>
    <FrameLayout
    android:id="@android:id/tabcontent"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
    </LinearLayout>
 </TabHost>     

第二个 XML 的代码

<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->

<item android:drawable="@drawable/ic_tab_artists_white"
      android:state_selected="true" 
      android:state_pressed="false" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/ic_tab_artists_grey" />
</selector>

.java 文件的代码

                Resources res = getResources(); 
                final TabHost MainTabHost = getTabHost();  
                TabHost.TabSpec spec; 
                Intent intent; 
                
                MainTabHost.getTabWidget().setStripEnabled(false);
                
                //call calendar Activity class
                intent = new Intent().setClass(this, CalendarForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost1)).setIndicator("Calendar",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                
                MainTabHost.addTab(spec);
                
                //call History Activity class
                intent = new Intent().setClass(this, HistoryForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost2)).setIndicator("History",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                MainTabHost.addTab(spec);
        
                //call Statistic Activity class
                intent = new Intent().setClass(this, StatisticForm.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                spec = MainTabHost.newTabSpec(res.getString(R.string.text_tabHost3)).setIndicator("Statistic",
                res.getDrawable(R.drawable.calendar_ic)).setContent(intent);
                MainTabHost.addTab(spec);
                
                //setbackground Style of tabHost                                       
                MainTabHost.setCurrentTab(0); 
                MainTabHost.getTabWidget().setWeightSum(3);
                final TabWidget tabHost=getTabWidget();   
                MainTabHost.setBackgroundResource(R.drawable.back_image);
                
                for (int j = 0; j < MainTabHost.getTabWidget().getChildCount(); j++) 
                {
                    ((TextView)tabHost.getChildAt(j).findViewById(android.R.id.title)).setTextColor(Color.parseColor("#FFFFFF"));
                    ((TextView)tabHost.getChildAt(j).findViewById(android.R.id.title)).setTextSize(16);
                }   

这就是我得到的。在这里,标签是方形的。现在我希望我的标签曲线位于右下角

【问题讨论】:

  • 您能否展示您所拥有的屏幕截图并模拟您想要实现的屏幕截图?
  • 我已经把快照发给你了,你检查一下

标签: android user-interface


【解决方案1】:

检查下面的问题你可以得到一个想法。

how-to-put-some-finishing-touches-rounded-edges.

另外一个解决方案是在标签上设置一个带有圆角的图像,我知道,俗气的解决方案,但有效。

tab = tabs.newTabSpec("tab_Busquedas");
tab.setContent(new Intent().setClassName("com.grapp", "com.grapp.homes").putExtras(bundle));
tab.setIndicator(null,null);
tabs.addTab(tab);
//here you set the image with rounded corners over the tab.
tabs.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.mytab_roundedcorners);

R.drawable.mytab_roundedcorners 将是一个选择器。

编辑:

放代码

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
<corners android:bottomLeftRadius="0dp"
     android:bottomRightRadius="0dp" 
     android:topLeftRadius="5dp"
     android:topRightRadius="5dp" />
</shape>

在drawable文件夹的xml文件中。然后使用 xml 文件作为选项卡的背景。这将是一个圆角。

【讨论】:

  • 没关系,我已经做到了,但还有其他选项..我不想将背景图像放在标签中..
  • @Anshuman:你浏览过给定的链接吗?这将在不粘贴图像的情况下完成圆角。
  • 是的,我已经浏览了那个链接,但是我无法找到我把那个 xml 代码放在我的代码中给出的链接的位置..你能帮我吗
  • @Anshuman:我已经编辑了我的答案。看看吧。在这个你需要不同形状的每个标签意味着你可以根据它编写不同的xml文件。
  • No hussain 它不起作用..在可绘制文件夹中创建 xml 文件后,我所做的是..MainTabHost.getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.style_round);但什么也没发生..代码有什么问题
猜你喜欢
  • 1970-01-01
  • 2017-12-18
  • 2014-03-09
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多