1

我有一个包含几个子元素( )的main.xml布局和其他布局。我想要做的是动态添加为可滚动列表元素。实际上确实做到了这一点,我使用如下循环:other.xmlTextView, ImageViewmain.xmlother.xml

    LayoutInflater layoutInflaterInstance = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view;
    View insertPoint = findViewById(R.id.linearLayoutToKeepOtherLayouts);


for (int i = 0; i < numberOfOtherXmlLayouts; i++) {
view = layoutInflaterInstance.inflate(R.layout.otherXml, null);

TextView firstValueOfOtherXml = (TextView) view.findViewById(R.id.first);
TextView secondValueOfOtherXml = (TextView) view.findViewById(R.id.second);
ImageView thirdValueOfOtherXml = (ImageView) view.findViewById(R.id.third);

firstValueOfOtherXml = setContent();
secondValueOfOtherXml = setContent();
thirdValueOfOtherXml = setContent();

((ViewGroup) insertPoint).addView(view, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

}

这就是我将动态内容添加到现有 main.xml 的方式。现在我想向 firstValueOfOtherXml/secondValueOfOtherXml/thirdValueOfOtherXml 添加一些活动,并尝试在 TextView/ImageView 中设置onClick事件,other.xml如下所示(在上部 for 循环中):

RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.otherXmlID);
                View.OnClickListener clickListener = new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        setContentView(R.layout.otherXml);
                        Intent myIntent = new Intent(getApplicationContext(), OtherActivity.class);
                        startActivityForResult(myIntent, 0);

                    }
};
relativeLayout.setOnClickListener(clickListener);

不幸的是,它仅适用于other.xml添加到main.xml. 我认为问题出在添加的 ID 中other.xml,我不会动态更改它(可能根据ifor 循环)。在任何添加动态布局元素的解决方案中,我都将不胜感激,因为它们的子元素的 ID 发生了动态变化,以使它们的识别成为可能。

希望它足够清楚。感谢帮助。

4

2 回答 2

0

您误解了 android 项目的模式:活动是每件事发生的上下文。每个 Activity 都有它的视图,可以使用 setContentView 设置,所以当您使用 setContentView 时,您实际上是在设置当前 Activity 的视图。

你要做的第一件事是启动一个 Activity(这是全局上下文),然后你在 Activity 中做任何你必须做的工作

所以你应该首先像你一样启动你的新活动:

     @Override
     public void onClick(View v) {
           Intent myIntent = new Intent(getApplicationContext(), OtherActivity.class);
           startActivityForResult(myIntent, 0);
     }

并且在您的 OtherActivity 类的 onCreate 方法中,您应该 setContentView 任何要应用于新的当前活动的布局 xml 文件。

xml文件仅用于描述视图。(实际上你可以使用Java来做到这一点)

于 2013-09-14T13:40:03.383 回答
0

由于我对活动“模式”的误解,YAT 的回答被接受了。我的问题的解决方案(切换到使用适当数据填充 layout.xml 的其他活动)是在 WeatherActivity.java 类中在 RelativeLayout 上使用 onClick="switchToWeekWeather" :

公共无效switchToWeekWeather(查看视图){

    TextView cityNameTextView = (TextView) ((RelativeLayout) view).getChildAt(1);
    Intent myIntent = new Intent(getApplicationContext(), CityWeatherActivity.class);
    String cityName = cityNameTextView.getText().toString();
    myIntent.putExtra("name", cityName);
    startActivity(myIntent);

}

在 CityWeatherActivity.java 中:

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle extras = getIntent().getExtras();
        String value = "";
        if (extras != null) {
            value = extras.getString("name");
        }

        LinearLayout cityView = (LinearLayout) getLayoutInflater().from(this).inflate(R.layout.cityview, null);
        TextView cityTextView = (TextView) cityView.findViewById(R.id.cityName);
        cityTextView.setText(value);
        setContentView(cityView);

    }

    public void backToMain(View view) {

            Intent myIntent = new Intent(getApplicationContext(), WeatherActivity.class);
            startActivity(myIntent);

    }
于 2013-09-15T14:14:30.253 回答