2011-12-28 7 views
0

私はtablyoutでアプリケーションを設計しています。私のデザインは大画面のデバイスでうまく見えますが、fxではうまくいきます。 HTC Wildfireのタブは画面の1/3を占めています。Androidタブのレイアウト - サイズを変更

小さな画面デバイス用のレイアウトファイルで小さいタブを作成すると、画像が消えます。 私の活動は次のようになります。

package dk.appsfabrikken.iphonetabs; 
import dk.appsfabrikken.iphonetabs.R; 
import android.app.TabActivity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Color; 
import android.graphics.drawable.StateListDrawable; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TabHost; 
import android.widget.TextView; 

public class MainActivity extends TabActivity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 

     setContentView(R.layout.main); 

     TabHost tabHost = getTabHost(); 
     TabHost.TabSpec spec; 
     Intent intent; 
     MyView view = null; 

     // Information Tab 
     intent = new Intent().setClass(this, InfoActivity.class); 
     view = new MyView(this, R.drawable.icon, R.drawable.icon, "Information"); 
     view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground)); 
     spec = tabHost.newTabSpec("Information").setIndicator(view).setContent(intent); 
     tabHost.addTab(spec); 

     // Pris tab 
     intent = new Intent().setClass(this, PriceActivity.class); 
     view = new MyView(this, R.drawable.icon, R.drawable.icon, "Priser"); 
     view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground)); 
     spec = tabHost.newTabSpec("Priser").setIndicator(view).setContent(intent); 
     tabHost.addTab(spec); 

     // Produkt liste Tab 
     intent = new Intent().setClass(this, ListeActivity.class); 
     view = new MyView(this, R.drawable.icon, R.drawable.icon, "Apps"); 
     view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground)); 
     spec = tabHost.newTabSpec("Apps").setIndicator(view).setContent(intent); 
     tabHost.addTab(spec); 

     //Kontakt tab 
     intent = new Intent().setClass(this, ContactActivity.class); 
     view = new MyView(this, R.drawable.icon, R.drawable.icon, "Kontakt"); 
     view.setFocusable(true); 
     view.setBackgroundDrawable(this.getResources().getDrawable(R.layout.selecttabbackground)); 
     spec = tabHost.newTabSpec("Kontakt").setIndicator(view).setContent(intent); 
     tabHost.addTab(spec); 

     tabHost.setCurrentTab(0); 
     tabHost.getTabWidget().getChildAt(0).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
     tabHost.getTabWidget().getChildAt(1).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
     tabHost.getTabWidget().getChildAt(2).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 
     tabHost.getTabWidget().getChildAt(3).getLayoutParams().height = LayoutParams.WRAP_CONTENT; 

    } 

    private class MyView extends LinearLayout { 
     ImageView iv; 
     TextView tv; 

     public MyView(Context c, int drawable, int drawableselec, String label) { 
      super(c); 
      iv = new ImageView(c); 
      StateListDrawable listDrawable = new StateListDrawable(); 
      listDrawable.addState(SELECTED_STATE_SET, this.getResources() 
        .getDrawable(drawableselec)); 
      listDrawable.addState(ENABLED_STATE_SET, this.getResources() 
        .getDrawable(drawable)); 
      iv.setImageDrawable(listDrawable); 
      iv.setBackgroundColor(Color.TRANSPARENT); 
      iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0)); 
      setGravity(Gravity.CENTER); 
      tv = new TextView(c); 
      tv.setText(label); 
      tv.setGravity(Gravity.CENTER); 
      tv.setBackgroundColor(Color.TRANSPARENT); 
      tv.setTextColor(Color.WHITE); 
      tv.setTextSize(12); 
      tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT, (float) 1.0)); 
      setOrientation(LinearLayout.VERTICAL); 
      addView(iv); 
      addView(tv); 
      setBackgroundDrawable(this.getResources().getDrawable(
        R.layout.selecttabbackground)); 
     } 
    } 
} 

は、アクティビティ内のデバイスの画面サイズでテストした後、iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0));を変更し、画像を小さくする方法はありますか?

私の主張を理解していない場合、私はそれをよりよく説明しようとします。

答えて

1

解決策が見つかりました。他人が使用できるように投稿します。 デンマークの解説には注意しないでください。

private class MyView extends LinearLayout { 
     ImageView iv; 
     TextView tv; 

     public MyView(Context c, int drawable, int drawableselec, String label) { 
      super(c); 
      iv = new ImageView(c); 
      StateListDrawable listDrawable = new StateListDrawable(); 
      listDrawable.addState(SELECTED_STATE_SET, this.getResources() 
        .getDrawable(drawableselec)); 
      listDrawable.addState(ENABLED_STATE_SET, this.getResources() 
        .getDrawable(drawable)); 
      iv.setImageDrawable(listDrawable); 
      iv.setBackgroundColor(Color.TRANSPARENT); 
      //Testing screen size and resize icons for small devices 
      if ((getResources().getConfiguration().screenLayout &  Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_SMALL) {  
       iv.setLayoutParams(new LayoutParams(30, 30, (float) 0.0)); 
      } 

      else{ 
      iv.setLayoutParams(new LayoutParams(50, 50, (float) 0.0)); 
      } 

      //Tekst og billeder centreres på skærmen 
      setGravity(Gravity.CENTER); 
      tv = new TextView(c); 
      tv.setText(label); 
      tv.setGravity(Gravity.CENTER); 
      tv.setBackgroundColor(Color.TRANSPARENT); 
      //Størrelse og tekstfarve bestemmes 
      tv.setTextColor(Color.WHITE); 
      tv.setTextSize(12); 
      tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT, (float) 1.0)); 
      setOrientation(LinearLayout.VERTICAL); 
      addView(iv); 
      addView(tv); 
      //Bestemmer hvordan en tab skal se ud når den markeres 
      setBackgroundDrawable(this.getResources().getDrawable(
        R.layout.selecttabbackground)); 
     } 
0

取得画面サイズ:

Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth(); 
    int height = display.getHeight(); 

移動レイアウトやXMLへのコードから、その要素!

+0

アドバイスありがとうございます。私はガイドを見つけることができる場合私はSEを行います:) しかし、活動の中でそれを行うことはできませんか? – Kano

+0

これはアクティビティ内で行う必要があります –

+0

はい、これはコードでのみ実行できます。しかし、あなたは可能な限りリソースに移動しています。 – Gangnus

関連する問題