1
我試圖構建我的第一個自定義gui元素,但當我嘗試在代碼中稍後更改外觀或適配器(使用Gallery)時遇到問題。充氣自定義圖庫佈局更改屬性失敗
我的問題:我不能改變自定義畫廊性質
我的實際代碼:
首先我創建一個XML這是小工具customGallery.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="@+id/toLeft"
android:background="@drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginBottom="1dip" />
<Gallery
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_toRightOf="@+id/toLeft"
android:spacing="40dip"
android:scrollbars="horizontal"/>
<ImageButton android:id="@+id/toRight"
android:background="@drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_toRightOf="@+id/gallery" />
</merge>
後來我創建test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lin_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.Test.GallerySlider
android:id="@+id/choose"
android:layout_span="2"
android:layout_width="300dip"
android:layout_height="wrap_content" />
</LinearLayout>
我的下一步是包含此自定義控件到我的項目,並從我的窗口小部件更改適配器:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout);
ViewStub st3 = new ViewStub(TestwidgetActivity.this);
LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineLayout.addView(st3,paramst3);
st3.setLayoutResource(R.layout.test);
View st3InflaView =st3.inflate();
GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose);
gSlider.setNewAdapter(new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"}));
}
這是Widgetclass我寫道:
public class GallerySlider extends RelativeLayout implements OnClickListener {
private ArrayAdapter<String> adapter;
private Gallery gallery;
private ImageButton toLeftBtn = null;
private ImageButton toRightbtn = null;
public GallerySlider(Context context) {
super(context, null);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
public void init(Context ctx){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
inflater.inflate(R.layout.customGallery, this, true);
toLeftBtn = (ImageButton) findViewById(R.id.toLeft);
toLeftBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() > 0){
gallery.setSelection(gallery.getSelectedItemPosition()-1);
}
}
});
toRightbtn = (ImageButton) findViewById(R.id.toRight);
toRightbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){
gallery.setSelection(gallery.getSelectedItemPosition()+1);
}
}
});
adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1",
"1")});
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setBackgroundResource(R.drawable.h_sliderl);
gallery.setAdapter(adapter);
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case R.id.toLeft: gallery.setSelection(gallery.getFocusedChild().getId()-1);
break;
case R.id.toRight: gallery.setSelection(gallery.getFocusedChild().getId()+1);
break;
}
}
public void setNewAdapter(ArrayAdapter<String> _adapter){
gallery.setAdapter(_adapter);
((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged();
}
}
如果我打電話setNewAdapter(ArrayAdapter _adapter)沒有什麼變化..。我也試圖改變畫廊的大小,但它也失敗了(nothig發生)。我的方法是否爲假?
問候馬塞爾
你好,我改變這一點。但調用setnewAdapter()沒有什麼變化的問題是實際存在的..我認爲notyfiyDataSetChange將解決問題。任何想法? – marcel
@marcel:Plz,使用更新修改您的問題中的代碼。 – Macarse
你好,我是sry ..我發現我的錯誤我現在使用查看st3InflaView = st3.inflate(); GallerySlider gSlider =(GallerySlider)st3InflaView.findViewById(R.id.choose);感謝您指點方向.... – marcel