开发者

Programmatically change a xml-created view

Is there a way to get the parameters from a XML view, modify some stuff on it and then use it as content view ?

Let's say I have a normal Lin开发者_运维百科earLayout and I want to make that work:

LinearLayout layout = (LinearLayout) findViewById(R.layout.main);
setContentView(layout);

Instead of :

setContentView(R.layout.main);


Yes.

To be more specific, we need more specific info from you.

Edit

You can, for example, do the following. Say you have in your xml specification a TextView:

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TextView
    android:id="@+id/mytv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="22sp"
    android:textStyle="bold"/>

</RelativeLayout>

Now you want to center horizontal the TextView programmatically:

setContentView(R.id.main);
TextView myTV = (TextView) findViewById(R.id.mytv);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) myTV.getLayoutParams();
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
myTV.setLayoutParams(lp);

You just set the contentview at the start, and don't need to set it again when you change the variables.


You can do anyything you want to the layouts even after setContentView. When you do operations like add items to a layout or set a background, the views in the screen are redrawn.

onCreate method is where you can modify layouts as it it about to begin drawing on to a screen.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜