我有一个像这样的TabHost:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabhost"
android:background="@drawable/tabs_bg">
<LinearLayout
android:id="@+id/LinearLayout01"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TabWidget
android:id="@android:id/tabs"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginBottom="5dip">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
</FrameLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我正在以编程方式向此TabHost添加标签,如下所示:
tabHost = (TabHost)findViewById(android.R.id.tabhost);
tabHost.setOnTabChangedListener(this);
/* tid1 is firstTabSpec Id. Its used to access outside. */
TabSpec firstTabSpec = tabHost.newTabSpec("tid1");
TabSpec secondTabSpec = tabHost.newTabSpec("tid2");
TabSpec ThirdTabSpec = tabHost.newTabSpec("tid3");
/* TabSpec setIndicator() is used to set name for the tab. */
/* TabSpec setContent() is used to set content for a particular tab. */ …Run Code Online (Sandbox Code Playgroud) 我有一系列静态图像,总共有超过500个图像呈现在drawable目录中.我需要制作一个动画(每秒加载大约20个图像).我希望它能够顺利运行并且没有内存异常.
我想这样做,2至3秒(40到60张图像)的图像应该加载到内存中并显示,然后它们应该关闭(释放内存),然后加载下2到3秒的图像.此技术可以防止内存不足异常.它只是一个想法,我不知道它是否是一个好主意.请引导我一些更好的想法与一些代码一起...如果我的想法更好,可以工作,那么请告诉我一些帮助代码来做到这一点.
在阅读回复并按照您的建议进行操作后,我编写了一些代码,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/llMain">
<ViewFlipper android:id="@+id/imageflipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerInside"
android:layout_gravity="center" />
</ViewFlipper>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是我做动画的代码:
public class Animation extends Activity {
ViewFlipper flipper;
int myIndex = 216;
private final Handler handler = new Handler();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
flipper=(ViewFlipper)findViewById(R.id.imageflipper);
doTheAutoRefresh();
//displayData();
}
private void doTheAutoRefresh() {
handler.postDelayed(new Runnable() {
public void run(){
displayData(); // this …Run Code Online (Sandbox Code Playgroud) 我读过这篇文章:
到目前为止,如果我们在XML中设置权重和宽度,但是我想以编程方式进行操作.我的布局(XML)中有一个表,我正在以编程方式向其添加行.在每一行中,我添加了2个文本视图.我试过这样做:
TableLayout tl = (TableLayout)findViewById(R.id.myTable);
for(int i = 0; i < nl.getLength(); i++){
NamedNodeMap np = nl.item(i).getAttributes();
TableRow trDetails = new TableRow(this);
trDetails.setBackgroundColor(Color.BLACK);
TextView tvInfo = new TextView(this);
tvInfo.setTextColor(Color.WHITE);
tvInfo.setGravity(Gravity.LEFT);
tvInfo.setPadding(3, 3, 3, 3);
tvInfo.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvInfo.setText("info");
TextView tvDetails = new TextView(this);
tvDetails.setTextColor(Color.WHITE);
tvDetails.setGravity(Gravity.LEFT);
tvDetails.setPadding(3, 3, 3, 3);
tvDetails.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
tvDetails.setText(np.getNamedItem("d").getNodeValue());
trDetails.addView(tvInfo, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
trDetails.addView(tvDetails, new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 0.50f));
tl.addView(trDetails,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
Run Code Online (Sandbox Code Playgroud)
但没有运气,它显示空白屏幕,但如果我用XML做,那么它显示完美.请帮我解决这个问题,我做错了?
谢谢