【问题标题】:tabwidget icon align texttabwidget 图标对齐文本
【发布时间】:2013-02-05 06:04:14
【问题描述】:

您好,我已经有一个在 android 中使用标签小部件的标签布局代码,每个标签都有自己的活动,但我得到了这个:

我怎样才能把红色的文字圈往下移?

这是我的代码:

主要活动:

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>

非常感谢。

【问题讨论】:

  • 表示你想关闭 tabwidget 或文本?我很困惑你的要求是什么???
  • 向下移动文本,以便清楚地显示图标和文本,但我不知道如何将文本向下移动?
  • 添加一些空间到
  • 不会将文本本身移动到整个标签中

标签: android android-layout android-tablelayout android-tabs


【解决方案1】:

我这样做是为了使图像和文本在标签上对齐:

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);

【讨论】:

  • 有效,但现在文本正在减少,如何为所有标签设置更高的高度?谢谢
  • 是的,我也有解决方案
  • 听起来不错,为什么会这样?我尝试了很多教程,至少对我来说都有同样的问题
猜你喜欢
  • 2012-10-13
  • 1970-01-01
  • 2018-12-26
  • 1970-01-01
  • 2019-10-01
  • 2011-03-07
  • 1970-01-01
  • 2012-08-15
相关资源
最近更新 更多