<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.testapp.MainActivity" >
<LinearLayout
android:id="@+id/mainlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>
package com.example.testapp;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] arr = {"베스트" , "신규상품" , "로켓 배송"};
LinearLayout headerlinear = new LinearLayout(getBaseContext());
headerlinear.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
headerlinear.setWeightSum(1);
headerlinear.setBackgroundColor(Color.WHITE);
headerlinear.setOrientation(LinearLayout.HORIZONTAL);
for(String title : arr) {
Button titlebtn = new Button(getBaseContext());
titlebtn.setLayoutParams(new LayoutParams(headerlinear.getWidth()/3-10, LayoutParams.WRAP_CONTENT));
// titlebtn.setBackgroundColor(Color.WHITE);
titlebtn.setText(title);
// titlebtn.setWidth();
titlebtn.setTextColor(Color.BLACK);
headerlinear.addView(titlebtn);
}
LinearLayout mainlinear = (LinearLayout) findViewById(R.id.mainlinear);
mainlinear.addView(headerlinear);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
맨위코드가 Main.xml 이고
아래코드가 생성하는 곳입니다.
버튼을 삼등분해서 Tab 처럼 만들어보고자 했지만 버튼 크기 조절이 생각보다 잘 되지 않아 질문드립니다.
xml 상에서 weight 0.3 으로 하면되지만 코드상으로는 어떻게 해야 될지 모르겠습니다.