0

在这里,我从另一个类和片段中调用了布局,如下所示

class example extends Activity{
      class2 btn;
      @Override
      public void onCreate(Bundle savedInstanceState) {
                 super.onCreate(savedInstanceState);
                 setContentView(R.layout.main);       
           btn.color(); 
      }
}
//Another class(class2)
public class class2 extends Activity{
    protected void color(){
      View inflatedView = getLayoutInflater().inflate(R.layout.main, null);
          LinearLayout layoutcolor=(LinearLayout) inflatedView.findViewById(R.id.linearcolor); 
     //some code
   }
}

我如何在 class2 中调用布局 id,因为我可以从上面的代码泛滥错误的类示例中显示它。

4

1 回答 1

-1
class example extends Activity {
    class2 btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = new Class2();
        btn.color(this);
    }
}

// Another class(class2)
public class class2 {
    protected void color(Activity mActivity) {
        LinearLayout layoutcolor = (LinearLayout) mActivity.findViewById(R.id.linearcolor);
        // some code
    }
}

更新:我创建了一个示例,它对我有用,只需检查您的实现。或发布完整代码。

package com.example.test;

import android.app.Activity;
import android.app.ListActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class sampleMediaPlayer extends ListActivity {
    // Toast mToast;

    private TextView selection;

    class2 btn;
    private static final String[] items = { "Computer Hardware", "Featured", "Information Technology",
            "Software", "Technical"

    };

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

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,
                items);
        setListAdapter(adapter);
        btn = new class2();
        btn.color(this);

    }

    public void onListItemClick(ListView parent, View v, int position, long id) {
        selection.setText(items[position]);
    }

    // Another class(class2)
    public class class2 {
        protected void color(Activity mActivity) {
            ListView layoutcolor = (ListView) mActivity.findViewById(android.R.id.list);
            layoutcolor.setBackgroundColor(Color.GREEN);
            Log.i("NIMISH", "HEy ");
        }
    }
}
于 2013-01-31T06:47:20.340 回答