国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android動態加載布局文件示例

2020-04-11 11:52:40
字體:
來源:轉載
供稿:網友

一、布局文件part.xml:

復制代碼 代碼如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="64dp"
        android:text="添加" />
    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10" >
        <requestFocus />
    </EditText>
</RelativeLayout>

二、通過后臺代碼生成前臺布局:

復制代碼 代碼如下:

package com.example.codeui;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout layout=new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);//顯示方向
        //將view對象添加到布局界面
        TextView textView =new TextView(this);
        textView.setText("Hello Code UI");
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
        //添加
        layout.addView(textView,params);
        //添加外部xml定義的布局
        View view = getPartView();
        layout.addView(view);  
        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);
        setContentView(layout, layoutParams);
        //setContentView(R.layout.activity_main);
        //采用代碼編寫效率高,但是很難維護
    }
    //通過加載xml文件將view添加到布局中
    public View getPartView() {
        //將xml布局文件生成view對象通過LayoutInflater
        LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //將view對象掛載到那個父元素上,這里沒有就為null
        return inflater.inflate(R.layout.part, null);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 定兴县| 万载县| 吉水县| 迁安市| 建德市| 手游| 丘北县| 互助| 曲松县| 宁化县| 顺义区| 军事| 梁河县| 华池县| 汉源县| 盐城市| 武威市| 宾阳县| 望奎县| 凭祥市| 尉氏县| 阿克| 铜陵市| 临沭县| 苏尼特左旗| 葵青区| 高雄县| 江口县| 阳东县| 双流县| 嘉禾县| 伊川县| 涡阳县| 三江| 万宁市| 图木舒克市| 大姚县| 涞水县| 同德县| 张家港市| 云霄县|