如何动态了解android当前的布局

如何动态了解android当前的布局,android,android-layout,Android,Android Layout,在我的活动中,有时我必须更改布局,而且我还必须更改选项菜单 So how can i know what is the current layout? 在每次“更改布局”时更新的活动/片段/任何内容中使用数据成员,自己跟踪它。活动类的示例代码- public class DynamicLayoutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) {

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

So how can i know what is the current layout?

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

活动类的示例代码-

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>

尝试执行以下操作:

  • 给你的布局一个ID

  • 使用findviewbyd方法查找它

  • 然后可以添加视图