我正在设计一个带有tablyout的应用程序。我的设计在大屏幕设备上看起来很好,但是在fx上。 HTC Wildfire,该选项卡几乎占据了屏幕的1/3。
如果我在小屏幕设备的布局文件上制作一个较小的标签,我的图像将会消失。 我的活动如下:
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));
并缩小图像?
如果您不理解我的观点,我会尝试更好地解释它。
答案 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));
}
答案 1 :(得分:0)
获取屏幕尺寸:
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
将布局及其元素从代码移动到xml!