Wednesday, August 6, 2014

android develop log 2 - custom control xml and code

1.android control set style by code

answer:



I do not believe you can set the style programatically. To get around this you can create a template layout xml file with the style assigned, for example in res/layout create tvtemplate.xml as with the following content:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This is a template"
style="@style/my_style" />

then inflate this to instantiate your new TextView:

TextView myText = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);

Hope this helps.



2.android remote icon from tabhost control


answer:


create one tab class:

public class NoIconTab extends LinearLayout {
public NoIconTab(Context c, int drawable, String label) {
super(c);

TextView tv = new TextView(c);

tv.setTextColor(getResources().getColorStateList(R.color.tab_text_color));
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
tv.setGravity(0x01);

setOrientation(LinearLayout.VERTICAL);

if (drawable != 0) {
ImageView iv = new ImageView(c);
iv.setImageResource(drawable);
addView(iv);
}
addView(tv);
}
}




then used it by code:

mTabHost.addTab(mTabHost.newTabSpec("call_log")
.setIndicator(new NoIconTab(this,0,this.getResources().getString(R.string.scene)))
.setContent(intent));

No comments: