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>

クラスタブ:

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

}

タブ.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) のようなことをしていますが、アプリケーションを起動したときにそのボタンが表示されません。私のアクティビティのコードにボタンを含まれたレイアウトに追加するにはどうすればよいですか?

4

1 に答える 1

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

上記のコードでは、キャプションテキストまたはレイアウトパラメータを定義していることがわかりません。そのため、コンテンツが欠落しているためにボタンが非表示になっている可能性があります。さらに、レイアウト、要素を確実に配置する方法、新しい要素が表示される位置を確認する方法を確認してください。

于 2013-01-31T14:43:11.510 に答える