android如何用代码实现界面ui

                          如何用代码实现界面ui

demo:

1.AndroidManifest.xml




    
        
            
                

                
            
        
    

2.activity_main.xml  没有应用此布局配置




    

3.MainActivity.java

package com.glsite.codeui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);

        TextView textView = new TextView(this);
        textView.setText(R.string.app_name);
        //textView.setId(34);
        LinearLayout.LayoutParams textParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(textView, textParams);
        setContentView(linearLayout, layoutParams);
    }
}

 

你可能感兴趣的:(java,android)