2011-12-28 53 views
0

我正在設計一個應用程序。我的設計在大屏幕設備上看起來很好,但在fx上。 HTC Wildfire,該選項卡佔據了屏幕的三分之一左右。安卓選項卡布局 - 更改大小

如果我在小屏幕設備的佈局文件上製作一個較小的選項卡,我的圖像將消失。 我的活動是這樣的:

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

感謝您的建議。我會如果我能找到一個指導:) 但是它不可能在活動內部做到嗎? – Kano 2011-12-28 22:42:40

+0

這必須在活動內完成 – 2011-12-29 02:22:39

+0

是的,這可以用代碼完成。但是你儘可能多地轉向資源。 – Gangnus 2011-12-30 17:42:06