2011-12-28 52 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