android设备中的选项卡高度设置不正确

android设备中的选项卡高度设置不正确,android,Android,我实现了tablayout,但它在emulator中无法正常工作。该高度未在设备中正确显示。有人能告诉我问题出在哪里吗 这是我的布局文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_hei

我实现了tablayout,但它在emulator中无法正常工作。该高度未在设备中正确显示。有人能告诉我问题出在哪里吗

这是我的布局文件

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

 <TabHost 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:background="@color/black"
        > 
   <RelativeLayout    
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"> 
         <FrameLayout android:id="@android:id/tabcontent" 
             android:layout_width="fill_parent"  
             android:layout_height="wrap_content" 
             android:layout_alignParentTop="true"  
             android:layout_above="@android:id/tabs" /> 
    <TabWidget android:id="@android:id/tabs" 
             android:layout_width="fill_parent"  
             android:layout_height="wrap_content" 
              android:layout_alignParentBottom="true"
                       /> 
          </RelativeLayout> 
    </TabHost>
    </LinearLayout>

这是我的java代码设置高度和宽度

for (int i =0; i<tabWidget.getChildCount(); i++) {
         //tabWidget.getChildAt(i).setBackgroundColor(R.color.black);
          tabWidget.getChildAt(i).getLayoutParams().height = height;
   tabWidget.getChildAt(i).getLayoutParams().width = width;
   RelativeLayout relLayout = (RelativeLayout)tabWidget.getChildAt(i); 
   TextView tv = (TextView)relLayout.getChildAt(1); 
   tv.setTextSize(10.0f);
   tv.setTypeface(myTypeface1);
}

for(int i=0;i这可能与模拟器和您的设备之间的屏幕密度差异有关。请检查-您需要获得当前设备的密度,如下所示:

DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float density = metrics.density;
接下来,根据屏幕上的“密度无关像素”部分计算必要的高度和宽度比例因子:

浮动比例系数=密度/160

最后,适当设置小部件的高度和宽度:

tabWidget.getChildAt(i).getLayoutParams().height = int ( height * scaling_factor );
tabWidget.getChildAt(i).getLayoutParams().width = int ( width * scaling_factor );

为什么要从代码设置宽度?