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 (버튼 자식)과 같은 작업을하지만 응용 프로그램을 시작할 때 해당 버튼을 볼 수 없습니다. 레이아웃을 포함하도록 내 활동 코드에 버튼을 추가하려면 어떻게해야합니까? 동적으로 버튼을 누르거나 다른보기를 추가하려면