我在谷歌地圖上創建了自定義視圖我想在頂部信息上添加什麼是用戶點擊的東西,並且我對自定義視圖有很大的問題,如何從活動訪問變量。自定義查看如何訪問變量?
public class Shop extends LinearLayout {
private TextView name;
public Shop(Context context) {
super(context);
setContentView();
}
public Shop(Context context, AttributeSet attrs) {
super(context, attrs);
setContentView();
}
public Shop(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setContentView();
}
private void setContentView() {
LayoutInflater layoutInflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.list_item_display_brand, this);
name = (TextView) view.findViewById(R.id.shop);
name.setText(".");
}
public void shopSetText() {
name.setText("PetShop");
}
}
,我加入到活動:
public class MapViewActivity extends Activity {
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.activity_map);
Shop map = new Shop(getApplicationContext());
map.shopSetText();
}
' 爲什麼我不能從shopSetText集的名字嗎?現在它只顯示「。」
謝謝,我那樣做,但它也沒有幫助。 奇怪的是,我可以改變它只從構造函數 – shiwon 2014-09-07 06:37:56