0

在这里,我将一个布局膨胀到另一个具有列表视图的布局中

public class MyListAdapter extends ArrayAdapter<Certs>{

        Context context;
        public MyListAdapter(Context context, int resourceId,List<Certs> items){
            super(context, resourceId, items);
            this.context = context;

        }
         /*private view holder class*/
        public class ViewHolder {
            ImageView imageView;
            TextView makeText;
            ProgressBar mPB;    
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
             //ViewHolder holder = null;



            LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.item_view, null);
                holder = new ViewHolder();

                holder.makeText = (TextView) convertView.findViewById(R.id.item_txtName);
                holder.imageView = (ImageView) convertView.findViewById(R.id.item_icon);
                holder.mPB = (ProgressBar) convertView.findViewById(R.id.progress);
                //holder.mPB.getIndeterminateDrawable().setColorFilter(0xFF4b86bb, android.graphics.PorterDuff.Mode.DST);

                convertView.setTag(holder);
            } else{
                holder = (ViewHolder) convertView.getTag();
            }

            Certs currentCert = myCerts.get(position);

            holder.makeText.setText(currentCert.getName().toString());
            Typeface face = Typeface.createFromAsset(getAssets(), "fonts/calligraphic.ttf");
            holder.makeText.setTypeface(face);

            holder.imageView.setImageResource(currentCert.getIconID());


            //holder.mPB.setProgress(10);
            holder.mPB.setId(currentCert.getPbID());

            return convertView;
        }


    }

通过这段代码,我可以扩展布局,但我不想在这里使用列表视图。我希望​​不同行中的多个图像和文本将显示在相同的线性布局上,而不使用列表视图。

4

2 回答 2

0

要在列表视图的 ImageView 中加载图像而不是尝试以下操作

Android 上用于异步图像加载和缓存的库

于 2013-10-31T07:36:08.190 回答
0

是的,有可能这样做。我已经创建了像你这样的相关代码......

package com.android.customlayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

    private Button mAddButton;
    private Button mDeleteButton;
    private LinearLayout mLinear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mAddButton = (Button) findViewById(R.id.add_button);
        mDeleteButton = (Button) findViewById(R.id.delete_button);
        mLinear = (LinearLayout) findViewById(R.id.child_linear);
        mAddButton.setOnClickListener(this);
        mDeleteButton.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {

        switch(v.getId()) {
            case R.id.add_button :
                View childView = getLayoutInflater().inflate(R.layout.custom_layout, null);
                mLinear.addView(childView);
                break;
            case R.id.delete_button :
                int childSize = mLinear.getChildCount();
                if(childSize != 0) {
                    mLinear.removeViewAt(childSize - 1);
                }
                break;
        }
    }  
}

在你的主要 xml 中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/add_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="29dp"
            android:text="Add" />

        <Button
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="36dp"
                        android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/add_button"
            android:text="Delete" />

        <LinearLayout android:id="@+id/child_linear"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/add_button"
            android:layout_marginTop="45dp"
            android:orientation="vertical" >
        </LinearLayout>

    </RelativeLayout>

制作另一个名为 custom Layout.xml 的 xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

</LinearLayout>

希望这会有所帮助.. :)

于 2013-10-31T06:24:28.570 回答