0

在我的活动中,有时我必须更改布局,而且我必须更改选项菜单。

So how can i know what is the current layout?
4

3 回答 3

1

自己跟踪它,在您的活动/片段/每次“更改布局”时更新的任何内容中使用数据成员。

于 2013-09-13T13:46:44.917 回答
0

尝试这样做:

  • 给你的布局一个 ID

  • 使用 findViewById 方法找到它

  • 然后你可以添加你的视图

.

于 2013-09-13T14:03:08.063 回答
0

活动类的示例代码-

public class DynamicLayoutActivity extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      // This will create the LinearLayout
      LinearLayout ll = new LinearLayout(this);
      ll.setOrientation(LinearLayout.VERTICAL);
      // Configuring the width and height of the linear layout.
      LayoutParams llLP = new LayoutParams(
        //android:layout_width='match_parent' an in xml
        LinearLayout.LayoutParams.MATCH_PARENT,
        //android:layout_height='wrap_content'
        LinearLayout.LayoutParams.MATCH_PARENT);
      ll.setLayoutParams(llLP);
      TextView tv = new TextView(this);
      LayoutParams lp = new LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
      tv.setLayoutParams(lp);
      //android:text='@string/c4r'
      tv.setText(R.string.c4r);
      //android:padding='@dimen/padding_medium'
      tv.setPadding(8, 8, 8, 8);
      ll.addView(tv);
      EditText et = new EditText(this);
      et.setLayoutParams(lp);
      et.setHint(R.string.c4r);
      et.setPadding(8, 8, 8, 8);

      ll.addView(et);
      Button bt = new Button(this);
      bt.setText(R.string.OtherActivity);
      bt.setPadding(8, 8, 8, 8);
      ll.addView(bt);
      //Now finally attach the Linear layout to the current Activity.
      setContentView(ll);
      //Attach OnClickListener to the button.
      bt.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View view) {
        Toast.makeText(getApplicationContext(),
          'This is dynamic activity', Toast.LENGTH_LONG).show();
       }
      });
     }
    }

并在 menifest.xml 添加-

<activity android:name='.DynamicLayoutActivity'
            android:label='@string/dynamic_layout_activity'>
            <intent-filter >
                <category android:name='android.intent.category.LAUNCHER'/>
            </intent-filter>
        </activity>
于 2013-09-13T13:58:23.970 回答