将代码中的一些元素添加到其他布局中

时间:2013-02-04 08:54:04

标签: android android-layout

我有两个类:一个是活动,第二个是扩展LinearLayout。这是活动代码:

public class SecondActivity extends Activity{

    private Button button;
    private ClassTabs tab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);


        Button next = (Button) findViewById(R.id.nextActivity);
        button = new Button(getApplicationContext());
        LayoutInflater inflater = LayoutInflater.from(this);
        tab = (ClassTabs) inflater.inflate(R.id.tab, null);
        tab.addTab(button);

        next.setOnClickListener(new OnClickListener() {

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


}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<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/tab1"
        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>

这是类扩展LinearLayout:

public class ClassTabs extends LinearLayout{

    Button button;
    public ClassTabs(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    public ClassTabs(Context context) {
        super(context);

    }
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        ((Activity)getContext()).getLayoutInflater().inflate(R.layout.tabview, this);
        setupViewItems();

    }

    private void setupViewItems() {
        button = new Button(getContext());      
    }

     public void addTab(Button child){
            LinearLayout tab = (LinearLayout) findViewById(R.id.tab);
            tab.addView(child);
     }

}

和xml:

<?xml version="1.0" encoding="utf-8"?>
<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>

现在您可以看到我有方法将一些子项添加到此布局中。我想从活动中为这个布局添加一些按钮。在该解决方案中,我得到nullPointer异常。如何使用其他类中的方法添加活动中的一些元素,在本例中为addTab()

编辑:这就是我想要的:在这种情况下我有一个活动“第二个活动”我想要使用“ClassTabs”中的布局。我ClassTabs我想有一个方法,它将添加按钮到布局,在活动中膨胀。我想在布局中运行活动我使用ClassTab的部分布局。下一步是从ClassTab添加到这个膨胀的布局,但我想在“第二个活动”的代码中执行此操作。我想让ClassTab中的方法添加按钮,因为我想在其他活动中使用该方法。

1 个答案:

答案 0 :(得分:0)

首先,你不必扩展一个LinearLayout只是为了动态添加子项,只要你不想为它提供某种行为,不要扩展它,而是使用普通的linearLayout并使用 addview()方法为其添加子项。

在您的代码中:

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 
    android:id="@+id/layout"
    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>

然后在你的活动中:

LinearLayout linearLayout;
@Override
public void onCreate(Bundle bundle) {
                 super.onCreate(bundle);
                 linearLayout = (LinearLayou) findViewById(R.layout.linearLayout);    
                }

然后当你想添加任何View“TextView / Button .....”时,只需使用:

Button button = new Button(this);
..... // modify the button
linearLayout.addChild(button);