2011-01-27 21 views
0

私はtablayoutを実装しましたが、エミュレータで正しく動作していません。デバイスの高さが正しく表示されていません。誰が問題が何であるか教えていただけますか?アンドロイドデバイスでタブの高さが正しく表示されない

これは私のレイアウトファイル

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

は、誰もがどのようにタブバーの高さと幅を設定する方法を教えてもらえますか?サンプルコードにこれはおそらく、エミュレータとデバイス間で画面密度の差に関係している

おかげ

+0

をなぜあなたはコードから幅を設定していますか? – Janusz

答えて

1

を与えます。 DisplayMetricsをチェック - 次のように現在のデバイスの密度を取得する必要があります:

DisplayMetrics metrics = new DisplayMetrics(); 
getWindowManager().getDefaultDisplay().getMetrics(metrics); 
float density = metrics.density; 

次に、Android dev site上の密度の独立したピクセルのセクションに応じて、必要な高さと幅のスケーリング係数を計算する:

float scaling_factor = density/160;

最後に、適切なウィジェットの高さと幅を設定します。

tabWidget.getChildAt(i).getLayoutParams().height = int (height * scaling_factor); 
tabWidget.getChildAt(i).getLayoutParams().width = int (width * scaling_factor); 
関連する問題