android LinearLayout java단에서 만들기
---- SampleLayoutCodeActivity.java
package com.example.administrator.myapplication_view;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.LinearLayout;
public class SampleLayoutCodeActivity extends Activity {
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setOrientation(LinearLayout.VERTICAL); // new 연산자로 LinearLayout을 만들고 방향 설정
Button button01 = new Button(this); // Button 생성 button01.setText("Button 01");
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // new 연산자로 레이아웃 안에 추가될 버튼에 대한 설정에 대한 파라메터 button01.setLayoutParams(params);
mainLayout.addView(button01);
setContentView(mainLayout);
}
}
-------- AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.administrator.myapplication_view">
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">
<activity android:name=".SampleLayoutCodeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>