2013-01-31 2 views
0

나는 활동이 있고 그 활동에서 버튼을 나의 레이아웃에 추가하고 싶습니다.다른 레이아웃에 버튼 추가

public class SecondActivity extends Activity{ 

    ClassTabs tabs; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.second_activity); 
     tabs = new ClassTabs(getApplicationContext()); 
     Button button = new Button(getApplicationContext()); 
     tabs.addTab(button); 
     Button next = (Button) findViewById(R.id.nextActivity); 


     next.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(SecondActivity.this, ThirdActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 
} 

XML :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <com.example.actionbartest.ClassTabs 
     android:id="@+id/tab" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     /> 

    <Button 
     android:id="@+id/nextActivity" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Button" 
     android:layout_marginTop="100dp" 
     /> 

</LinearLayout> 

ClassTab :

public class ClassTabs extends LinearLayout{ 

    Button button = new Button(getContext()); 
    public ClassTabs(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     init(context); 
    } 
    public ClassTabs(Context context) { 
     super(context); 
     init(context); 
    } 
// @Override 
// protected void onFinishInflate() { 
//  super.onFinishInflate(); 
//  //((Activity)getContext()).getLayoutInflater().inflate(R.layout.tabview, this); 
//  LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
//  inflater.inflate(R.layout.tabview, this); 
//  
// } 

    private void init(Context context) { 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.tabview, this); 

     } 
    public void addTab(Button child){ 
      LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View view = inflater.inflate(R.layout.tabview, this); 
      LinearLayout tab = (LinearLayout) view.findViewById(R.id.tab); 
      tab.addView(child); 
    } 

} 

tab.xml :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tab" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:orientation="horizontal" 
    android:background="@color/blue" 
    > 





</LinearLayout> 

당신은 내가 활동 어디를 볼 수 있듯이 이 코드 다른 레이아웃을 포함 시켰습니다. 코드에서 다른 레이아웃 (ClassTab)에 단추를 추가하고 싶습니다. 메소드 addTab (버튼 자식)과 같은 작업을하지만 응용 프로그램을 시작할 때 해당 버튼을 볼 수 없습니다. 레이아웃을 포함하도록 내 활동 코드에 버튼을 추가하려면 어떻게해야합니까? 동적으로 버튼을 누르거나 다른보기를 추가하려면

답변

0

, 다음 시도 : 위의 코드에서

//Create a button 
Button myButton = new Button(this); 
myButton.setText("Test"); 

// Get the layout to add the button to 
LinearLayout layout = (LinearLayout)findViewById(R.id.tab); 

// If necessary add some paramters 
LayoutParams layout_params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 

// Finally add the button to the layout 
layout.addView(myButton, layout_params); 

를, 난 당신이 캡션 텍스트 또는 레이아웃 매개 변수를 정의하는 것을 볼 수 있으므로 버튼을하지 않습니다 누락 된 콘텐츠로 인해 보이지 않을 수 있습니다. 레이아웃을 자세히 살펴보면서 요소가 어떻게 배치되는지, 새 요소가 눈에 보이는 위치에 있는지 살펴보십시오.

관련 문제