1.如果软件的界面在运行期根据不同的条件显示不同的样子,通过编码实现软件界面
2.通过软件编码方式实现xml布局文件
<LinearLayout 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" >
<TextView
android:id="@+id/textView"
style="@style/childStyle.subChildStyle"
android:text="@string/hello_world"
/>
</LinearLayout>
代码
package com.example.sytletheme;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
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
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* textView 或layout都继承ViewGroup
*/
//创建一个LinearLayout
LinearLayout linearLayout = new LinearLayout(this);
//设置linearLayout布局属性--垂直
linearLayout.setOrientation(LinearLayout.VERTICAL);
//创建一个textview
TextView textView = new TextView(this);
//添加显示内容
textView.setText(R.string.hello_world);
//设置textview的宽高属性
ViewGroup.LayoutParams textParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//把textview添加到linearlayout当中
linearLayout.addView(textView, textParams);
//添加xml文件里的固定布局View
View partView = getPartView();
linearLayout.addView(partView);
//设置linearlayout布局的宽和高
ViewGroup.LayoutParams LayoutParams = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
setContentView(linearLayout,LayoutParams);
}
private View getPartView(){
//运用布局填充服务获取view
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//根据资源ID得到对象
return inflater.inflate(R.layout.part,null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}