a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 系統 > Android > 正文

Android布局――Preference自定義layout的方法

2020-04-11 12:15:21
字體:
來源:轉載
供稿:網友
導語:PreferenceActivity是一個方便設置管理的界面,但是對于界面顯示來說比較單調,所以自定義布局就很有必要了。本文舉例說明在Preference中自定義layout的方法。筆者是為了在設置中插入@有米v4廣告條才研究了一晚上的。

正文:首先PreferenceScreen是一個xml文件于res/xml目錄下,不屬于layout文件。要插入layout,有兩種方法。
1.使用Preference的android:@layout屬性
1)xml文件中preference的添加
復制代碼 代碼如下:

<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>

2)layout.xml
復制代碼 代碼如下:

<?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="wrap_content"
>
<!-- 有米廣告 -->
<akai.settings.YoumiAd
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
/>
</LinearLayout>

3)由于v4版本的廣告條需要自己定義layout載體,所以這里重載了一個LinearLayout,自定義布局也是如此。

在YoumiAd.java中處理布局文件,其他布局類似,不再敘述。
復制代碼 代碼如下:

public class YoumiAd extends LinearLayout{
public YoumiAd(Context context) {
super(context);
init(context);
}
public YoumiAd(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
//init youmi ad
AdView adView = new AdView(context, AdSize.SIZE_320x50);
this.addView(adView);
adView.setAdListener(new AdViewLinstener() {
@Override
public void onSwitchedAd(AdView arg0) {
Log.i("Youmi", "廣告條切換");
}
@Override
public void onReceivedAd(AdView arg0) {
Log.i("Youmi", "請求廣告成功");
}
@Override
public void onFailedToReceivedAd(AdView arg0) {
Log.i("Youmi", "請求廣告失敗");
}
});
}

}

2.將layout添加到Activity中 setContentView(R.layout.youmi_ad)以自定義內容
1)xml文件中preference的添加
復制代碼 代碼如下:

<Preference android:layout="@layout/youmi_ad" android:key="youmi_ad"/>

2)layout.xml,需要添加一個ListView控件,且id為list,不然不能運行,應該是由于PreferenceActivity是一個List的原因吧。
復制代碼 代碼如下:

<?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="wrap_content"
>
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:layout_weight="1"
/>
<!-- 有米廣告 -->
<LinearLayout
android:id="@+id/adLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
/>
</LinearLayout>

3)在Activity中的onCreate添加setContentView(R.layout.youmi_ad);然后就可以使用findViewById去獲取自定義布局下的控件了。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲日韩欧美一区二区在线 | 涩婷婷| 中文字幕在线电影观看 | 午夜免费小视频 | 国产精品久久久久久久 | 99国内精品久久久久久久 | 国产一区二区三区视频观看 | 91精品久久久久久久99 | japan护士性xxxⅹhd | 国产九九精品视频 | 蜜臀久久99精品久久久久久宅男 | 国产一区久久 | 欧美日韩在线免费 | 国产成人8x视频一区二区 | 日韩精品无码一区二区三区 | 亚洲香蕉视频 | 特级丰满少妇一级aaaa爱毛片 | 成人精品 | 能看的av | 国产精品18 | 伊人电影综合 | 成人午夜免费视频 | 国产一区二区精品在线 | 国产精品777一区二区 | 精品一二三 | 亚洲色图第一区 | 免费黄色在线 | 久久国精品| 一区二区三区视频免费观看 | 久久精品99 | 在线视频91 | 欧洲精品一区二区 | 毛片入口| 亚洲成人一区二区三区 | 久久日本视频 | 四虎网址| 亚洲成人一区二区 | 在线免费观看黄色小视频 | 电影91久久久 | 亚洲一区二区黄 | 一区二区三区国产 |