这里我从另一个类和片段调用布局如下
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中调用layout id,我可以从类示例上面的代码泛洪错误中显示它。
答案 0 :(得分:-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 ");
}
}
}