膨胀自定义图库布局更改属性失败

时间:2011-11-29 10:28:42

标签: android android-gallery

我尝试构建我的第一个自定义gui元素但是当我稍后在代码中尝试更改外观或适配器(使用Gallery)时出现问题。

我的问题:我无法更改自定义图库属性

我的实际代码:

首先,我创建一个XML,它是widget 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>

我的下一步是将这个自定义Widget包含到我的项目中,并从我的Widget中更改适配器:

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)没有任何改变..我也试图改变画廊的大小但它也失败了(不会发生)。我的方法是假的吗? 问候马塞尔

1 个答案:

答案 0 :(得分:0)

我可以检测到的第一件事是您正在创建自定义视图两次。

设置布局时,ViewStub会首次创建。 第二个,当你膨胀contentView时,它不会被添加到R.layout.test

您正在将适配器设置为第二个自定义视图,该视图未添加到视图层次结构中。