마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

코드상으로 버튼 weight 0.3 구현 질문드려요.

0 추천
<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 으로 하면되지만 코드상으로는 어떻게 해야 될지 모르겠습니다.

 

 

pleasemehelp (150 포인트) 님이 2015년 7월 7일 질문

1개의 답변

0 추천
http://stackoverflow.com/questions/13016216/how-to-programmatically-set-weight-for-button

 

layout param 에 weight 값을 3개다 1로 주세요 sum 은 빼셔도 됩니다.
aucd29 (218,390 포인트) 님이 2015년 7월 7일 답변
감사합니다. 한가지 더 여쭤봐도 될가요?
new Button으로 반복문으로 3개를 생성했는데 각각 다른 버튼을 참조하려면 어떻게 해야하나요...
메소드에서 세개의 버튼에 동일한 색깔을 주고 싶습니다.
각각 다른 버튼을 참조하려면 <<= 이게 먼 뜻인가요?

메소드에서 세개의 버튼에 동일한 색깔을 주고 싶습니다
btn.setBackground 관련된 method 들이 존재하오니 검색해보세요
http://developer.android.com/reference/android/widget/Button.html
...