gpt4 book ai didi

android - 选项卡小部件图标对齐文本

转载 作者:行者123 更新时间:2023-11-29 00:32:23 25 4
gpt4 key购买 nike

你好,我已经在 android 中使用选项卡小部件进行选项卡布局的代码,每个选项卡都有自己的 Activity ,但我得到了这个:

如何向下移动红色的文本圆圈?

这是我的代码:

主要 Activity :

package com.example.androidtablayout;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class AndroidTabLayoutActivity extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TabHost tabHost = getTabHost();

// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("En Vivo");
photospec.setIndicator("En Vivo", getResources().getDrawable(R.drawable.icon_songs_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);

// Tab for Songs
TabSpec songspec = tabHost.newTabSpec("Twitter");
// setting Title and Icon for the Tab
songspec.setIndicator("Twitter", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent songsIntent = new Intent(this, SongsActivity.class);
songspec.setContent(songsIntent);

// Tab for Videos
TabSpec videospec = tabHost.newTabSpec("Contactenos");
videospec.setIndicator("Contactenos", getResources().getDrawable(R.drawable.icon_videos_tab));
Intent videosIntent = new Intent(this, VideosActivity.class);
videospec.setContent(videosIntent);

// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
tabHost.addTab(songspec); // Adding songs tab
tabHost.addTab(videospec); // Adding videos tab
}
}

ma​​in.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>

非常感谢。

最佳答案

我这样做是为了使图像和文本在选项卡上对齐:

for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) 
{
TextView tv = (TextView) tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.title);
tv.setTextColor(Color.WHITE);
tv.setPadding(0, 0, 0, 5);
tv.setShadowLayer(2, 2, 2, Color.BLACK);
}

根据您的要求设置填充。

设置标签的高度:

LinearLayout.LayoutParams tab1=(LinearLayout.LayoutParams) getTabHost().getTabWidget().getChildAt(0).getLayoutParams();
LinearLayout.LayoutParams tab2=(LinearLayout.LayoutParams) getTabHost().getTabWidget().getChildAt(1).getLayoutParams();
LinearLayout.LayoutParams tab3=(LinearLayout.LayoutParams) getTabHost().getTabWidget().getChildAt(2).getLayoutParams();
LinearLayout.LayoutParams tab4=(LinearLayout.LayoutParams) getTabHost().getTabWidget().getChildAt(3).getLayoutParams();
LinearLayout.LayoutParams tab5=(LinearLayout.LayoutParams) getTabHost().getTabWidget().getChildAt(4).getLayoutParams();

tab1.setMargins(10, 10, 5, 0);
tab2.setMargins(5, 10, 5, 0);
tab3.setMargins(5, 10, 5, 0);
tab4.setMargins(5, 10, 10, 0);
tab5.setMargins(0, 10, 10, 0);

关于android - 选项卡小部件图标对齐文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14701165/

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