我试图创建一个标签主机,如所附的屏幕截图所示,但与图标图像和文本对齐问题 - 图像需要在文本上方对齐,但所有尝试失败;Android的 - 与tabhost和对齐imageview和textview的问题
PM For Image if unable to see link here
我已经包含了一些示例代码,但林无法强制文本的图像顶部对齐 - 它只是如果我增加layout_height在tab_indicator文件大于66dip但是我在有限的工作原理空间和提供的图像。如果你不能看到图像/链接,图像不会被强制在文本上方,所以图像的一部分在文本后面!
tab_indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="88.0dip"
android:layout_height="66.0dip"
android:layout_weight="1.0">
<include layout="@layout/tab_indicator_portrait" />
</FrameLayout>
tabindicator_protrait.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tab_indicator_portrait"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="3.0dip"
android:paddingTop="3.0dip" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
android:layout_marginTop="3.0dip" />
<TextView
android:id="@+id/title"
style="?android:attr/tabWidgetStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="@color/tab_indicator_text"
android:textSize="12.0dip" />
</FrameLayout>
tab_host.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:id="@+id/topLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="0.0dip" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="0dip"
android:padding="0dip" />
</LinearLayout>
</TabHost>
有几个原因,我得到了它的设置是这样;
- 的tab_indicator.xml文件还可以包含一个景观文件,如果我需要处理不同的景观,即未来的版本根据市场反馈/测试
- 允许应用程序有一个通用的外观和感觉,因为我只想要当用户在标签间移动时改变的图标 - 在4.0.4中有tabhost的问题,这看起来像是一种更清晰的处理方式。
我试过一些代码在我的TabActivity来处理基于屏幕大小的布局大小;
private void detectScreen()
{
Display localDisplay = getWindowManager().getDefaultDisplay();
if (localDisplay.getWidth() < localDisplay.getHeight())
{
int dips = 45;
TabHost localTabHost = getTabHost();
DisplayMetrics localDisplayMetrics = getResources().getDisplayMetrics();
for (int i = 0; i < localTabHost.getTabWidget().getTabCount(); i++)
{
View localView = localTabHost.getTabWidget().getChildTabViewAt(i);
localView.getLayoutParams().height = ImageUtils.convertDIPToPixels(localDisplayMetrics, (float)dips);
}
}
}
但同样,改变布局的大小确实使图像变小,但不与文本对齐。
清单文件还包含以下内容;
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true"/>
我测试过的tab_indicator_protait.xml改变的FrameLayout到的LinearLayout - 这只是删除了文本和显示图像! – user1859465