8月17日

动态创建并设置控件属性

AbsListview.LayoutParams lp = AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,50);

TextView tv = new TextView(context);

tv.setLayoutParams(lp);

 

 

关于在AlertDialog中显示自定义的视图。
一般情况下,用LayoutInflater填充自己显示的视图。
如R.layout.dothing.这里要注意一个问题。
如果我们只想在弹出的对话框中显示一个简单的控件。如
就一个EditText时,最好直接在要填充的布局里就这一个
控件。一般情况,直接新建的layout布局文件都有一个默认
的布局,如线性布局,如果不注意到话,在这个线性布局
中直接添加要定义的EditText。那么就会有如下问题。
在这个布局文件中,EditText的parent是LinearLayout。
而在我们自定义的对话框中,该EditText的parent是
AlertDialog。就会报错。所以如果我们需要在对话框
中自定义的布局较为简单,最好直接在要填充的布局
文件中就只有这一个控件


一般的在一个Activity中的findViewById操作,这里
findViewById所find的view是在该Activity的
setContentView(R.layout.xxx)中设置的view。
并不是在所有的layout中都会find。注意!!!

如果一个Activity中出现的所有的字体都一样,

那么可以定义一个style,然后再该style中添加一项android:textColor=xxx

并且在该Activity的onCreat方法中调用setTheme(R.style.***);

注意这个setTheme方法要在setContentView之前调用。

并且能全局设置的只是字体颜色,字体大小就不行。

 

背景选择器selector使用方法

XML代码:

在res/drawable下面新建mybutton_background.xml文件,内容如下:

<?xml version=”1.0″ encoding=”utf-8″?>

<selector xmlns:android=”http://schemas.android.com/apk/res/android“>

<item android:state_focused=”true”
android:state_pressed=”false”
android:drawable=”@drawable/yellow”
/>
<item android:state_focused=”true”
android:state_pressed=”true”
android:drawable=”@drawable/green” />

<item android:state_focused=”false”
android:state_pressed=”true”
android:drawable=”@drawable/blue”/>
<item android:drawable=”@drawable/grey” />

</selector>

这里面就定义了在不同状态下的显示图片,然后在layout里面定义Button的时候,
指定它的background为这个mybutton_background


<Button android:id=”@+id/btn”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/mybtn”
android:background=”@drawable/mybutton_background” />
</LinearLayout>

这种方式开发比较简单,适合做一些风格一致的Button,设置成同一个background就可以了。

posted on 2012-08-17 22:35  lightman_21  阅读(135)  评论(0)    收藏  举报

导航