【问题标题】:How to dynamically add custom layout with dynamically set id?如何使用动态设置的 id 动态添加自定义布局?
【发布时间】:2013-09-14 11:43:10
【问题描述】:

我有一个 main.xml 布局和其他布局 other.xml 包含几个子元素 (TextView, ImageView)。我想要做的是动态添加到main.xml 作为可滚动列表other.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 中的 other.xml 中设置 onClick 事件,如下所示(在上部 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);

不幸的是,它仅适用于添加到 main.xml 的第一个 other.xml 布局。我认为问题出在添加的 other.xml 的 ID 中,我没有动态更改它(可能根据 for 循环中的 i)。我将不胜感激添加动态布局元素的任何解决方案,这些元素具有动态更改的子元素 ID,以使其识别成为可能。

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

【问题讨论】:

    标签: java android android-layout android-intent android-listview


    【解决方案1】:

    你误解了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来做)

    【讨论】:

    • 你说的很明显。我不问如何更改为其他活动,因为它不是问题。也许我误导性地使用了单词活动来对抗事件。与主要活动类我结合了由其他视图组成的 main.xml。好像主要是联系人列表(xx),他们的视图元素是每个联系人(图像+描述)。作为对列表联系人之一的 onclick 操作的响应,我想转到其他视图(是的,切换活动并与其布局连接)。问题是我怎么知道点击了哪个元素(xx)。我认为为 every(xx) 设置唯一 ID 可以解决这个问题。
    • 但是在您提供的代码中,在您在切换活动之前使用 setContentView 的 onClick 方法中,为什么要在离开活动时设置活动的布局?至于ID,可以从onClick方法的View参数view.getID()中获取,通过intent.putExtra传递给其他Activity,在OtherActivity中处理
    • 我的回答中问题的解释和解决方案。感谢您的提示;)
    【解决方案2】:

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

    public void 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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-20
      • 2012-02-14
      • 2021-06-03
      • 2015-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      相关资源
      最近更新 更多