动态android布局背景

时间:2014-07-31 15:12:53

标签: android xml listview android-fragments

我有这个片段与ListView。

如何将背景设置为与在ImageView中上传的图像相同,以便我可以动态填充每行,并从XML文件中提取相同的背景图像。 我的图片的网址链接位于我的XML文件中,我正在使用XML解析器...

enter image description here

目前,我正在使用item_shadow.xml中的布局获得此白色背景(我想填充):

<?xml version="1.0" encoding="utf-8"?>
   <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
     <item >
        <shape 
          android:shape="rectangle">
              <solid android:color="@android:color/darker_gray" />
              <corners android:radius="5dp"/>
        </shape>
     </item>
     <item android:right="1dp" android:left="1dp" android:bottom="2dp">
        <shape 
          android:shape="rectangle">
              <solid android:color="@android:color/white"/>
              <corners android:radius="5dp"/>
        </shape>
     </item>
   </layer-list>

ListView行填充以下内容:

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/item_shadow"
    android:padding="10dp" >

    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

   <ImageView
       android:id="@+id/iconUrl"
       android:layout_width="match_parent"
       android:layout_height="80dp"
       android:layout_alignParentLeft="true"
       android:layout_alignParentRight="true" />

</RelativeLayout>

这是我的片段:

public class Preporuceno extends Fragment  {

    private AppAdapter mAdapter;
    private ListView siteList;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.i("mobAppModel", "OnCreate()");    
    View rootView = inflater.inflate(R.layout.activity_preporuceno, container, false);


    siteList = (ListView) rootView.findViewById(R.id.listView1);   

    siteList.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos,long id) {
            String url = mAdapter.getItem(pos).getstoreURL();
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

        }
    });

        if(isNetworkAvailable()){
            Log.i("mobAppModel", "starting download Task");
            AppDownloadTask download = new AppDownloadTask();
            download.execute();
        }else{
            mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));
            siteList.setAdapter(mAdapter);
        }

        return rootView;
    }

    private boolean isNetworkAvailable() {
        ConnectivityManager connectivityManager 
              = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } 
    private class AppDownloadTask extends AsyncTask<Void, Void, Void>{
        @Override
        protected Void doInBackground(Void... arg0) {
            //Download the file
            try {
                Downloader.DownloadFromUrl("https://dl.dropboxusercontent.com/s/te0c0s7y7zr79tm/kategorijeXML.xml", getActivity().openFileOutput("XMLsource.xml", Context.MODE_PRIVATE));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }

            return null;
        } 
        @Override
        protected void onPostExecute(Void result){
            //setup our Adapter and set it to the ListView.
            Activity attached = getActivity();
            if (attached != null) {
            mAdapter = new AppAdapter(getActivity().getApplicationContext(), -1, XMLsourcePullParser.getmobAppModel(getActivity()));

                siteList.setAdapter(mAdapter);

            Log.i("mobAppModel", "adapter size = "+ mAdapter.getCount());}
        }

    }

 }

编辑编辑

帮助我的解决方案就是设置android:scaleType =&#34; fitXY&#34;在ImageView。

0 个答案:

没有答案