gpt4 book ai didi

带有图像的 Android 自定义选项卡选择指示器

转载 作者:行者123 更新时间:2023-11-30 02:57:43 25 4
gpt4 key购买 nike

我是 android 的新手,我真的很想知道是否可以像图像中那样自定义选项卡选择(URL:http://i.stack.imgur.com/Hg5sT.png)。正如您在图像中看到的,当选择一个选项卡时,有一个向下的小三角形(图像)。如果是,这怎么能实现 xml/java。我尝试在选项卡中添加背景图片,但没有按预期显示。我在网上研究了很多,但找不到如何做到这一点。

感谢您的耐心配合。 http://i.stack.imgur.com/Hg5sT.png

最佳答案

如果要实现此目的,您必须为单个选项卡创建两个图像。并根据选择更改它们

主要 Activity

public class MainActivity extends TabActivity
{
TabHost tabHost;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

tabHost = getTabHost();

TabSpec spec;

Intent intent;

//Home Tab
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.home, null); // R.layout.home is the home.xml where you have entered two image

intent = new Intent(MainActivity.this, Firstclass.class);

spec = tabHost.newTabSpec("HOME").setIndicator(view1)
.setContent(intent);

tabHost.addTab(spec);

//Calendar Tab
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.calendar_tab, null);

intent = new Intent(MainActivity.this, Calendar.class);

spec = tabHost.newTabSpec("CALENDAR").setIndicator(view2)
.setContent(intent);

tabHost.addTab(spec);

主页.xml

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

<ImageView
android:id="@+id/home_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:layout_centerInParent="true"
android:background="@drawable/selector1" />

</RelativeLayout>

@drawable/selector1

选择器1.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:drawable="@drawable/home_selected" android:state_selected="true"></item>
<item android:drawable="@drawable/home_unselect" android:state_selected="false"></item>

</selector>

在 state_selected 图像将被 home_selected 但在未选择状态图像将被替换你必须为每个选项卡创建这两个 xml 文件。您的问题将得到解决。

关于带有图像的 Android 自定义选项卡选择指示器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23018875/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com