Android UI教程 - 代码中的Android UI
2018-01-09 14:51 更新
Android UI教程 - 代码中的Android UI
第一个例子演示了如何完全在代码中构建UI。
例子
从Android Studio生成项目,将以下代码剪切并粘贴到 MainActivity.java
。
package com.java2s.app; //from w w w . j a v a 2 s. c om import android.app.Activity; import android.os.Bundle; import android.view.ViewGroup.LayoutParams; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout nameContainer = new LinearLayout(this); LinearLayout addressContainer = new LinearLayout(this); LinearLayout parentContainer = new LinearLayout(this); nameContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); nameContainer.setOrientation(LinearLayout.HORIZONTAL); TextView nameLbl = new TextView(this); nameLbl.setText("Name: "); TextView nameValue = new TextView(this); nameValue.setText("Java2s.com"); nameContainer.addView(nameLbl); nameContainer.addView(nameValue); addressContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); addressContainer.setOrientation(LinearLayout.VERTICAL); TextView addrLbl = new TextView(this); addrLbl.setText("Address:"); TextView addrValue = new TextView(this); addrValue.setText("Main Street"); addressContainer.addView(addrLbl); addressContainer.addView(addrValue); parentContainer.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); parentContainer.setOrientation(LinearLayout.VERTICAL); parentContainer.addView(nameContainer); parentContainer.addView(addressContainer); setContentView(parentContainer); } }
以上内容是否对您有帮助:
更多建议: