2013-01-31 51 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(Button child)的东西,但是当我启动我的应用程序时,我看不到那个按钮。我如何做到这一点,在我的活动代码中添加按钮到包含布局?

回答

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); 

在上面代码中,我没有看到你定义的标题文本或布局参数,使按键由于缺少内容,可能会隐藏起来。进一步看看你的布局,它如何安排它的元素,以确保新元素得到一个明显的位置。