Android动态设置布局里如何设置子布局的位置?-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Android动态设置布局里如何设置子布局的位置?

就是平时新闻或者广告的定时滑动,广告底边不是有一条半透明的带和可以跟随滑动的几个圆点,我用LayoutParams写的布局,结果出来效果 圆点全叠在一起了,那半透明的带也在顶部去了,我想知道怎么才能把圆点分开且半透明的带也在底部?谢谢了!

    public class FragmentDemo extends Fragment {
    private ImageView mImageIds[];
    private int index=0;//记录选中的图片位置
    private static final int IMAGE_COUNT=3;//小圆点个数
    Gallerys_Advertisement gallery;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    //整个Tabs之下的容器
    LayoutParams SclayoutParams=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.MATCH_PARENT);
    ScrollView scrollView=new ScrollView(getActivity());
    scrollView.setLayoutParams(SclayoutParams);
    //界面布局容器一
    RelativeLayout R2Layout=new RelativeLayout(getActivity());
    LayoutParams R2params=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    R2Layout.setLayoutParams(R2params);
        //画廊
        LayoutParams galleryParams=new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
        gallery=new Gallerys_Advertisement(getActivity());
        gallery.setFadingEdgeLength(0);//消除图片两边的阴影
        gallery.setSpacing(0);
        gallery.setLayoutParams(galleryParams);
        //界面布局容器二
        RelativeLayout relativeLayout=new RelativeLayout(getActivity());

        //原来Layoutparams设置布局要使用Margins必须要加容器类型在Params语句前如下:RelativeLayout。
        RelativeLayout.LayoutParams RRparams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT,50);
        //      RRparams.setMargins(0, 0, 0, 0);
        relativeLayout.setBackgroundColor(getResources().getColor(R.drawable.beijingse));//设置自定义背景色
        relativeLayout.setLayoutParams(RRparams);
        relativeLayout.setGravity(Gravity.CENTER);//确定圆点位置居中

        //三张图
        LayoutParams IVparams1=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        ImageView imageView1=new ImageView(getActivity());
        imageView1.setImageDrawable(getResources().getDrawable(R.drawable.ic_dot_focused));//重点:添加自定义图片
        imageView1.setLayoutParams(IVparams1);



        LayoutParams IVparams2=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        ImageView imageView2=new ImageView(getActivity());
        imageView2.setImageDrawable(getResources().getDrawable(R.drawable.ic_dot_normal));//重点:添加自定义图片
        imageView2.setLayoutParams(IVparams2);



        LayoutParams IVparams3=new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        ImageView imageView3=new ImageView(getActivity());
        imageView3.setImageDrawable(getResources().getDrawable(R.drawable.ic_dot_normal));//重点:添加自定义图片
        imageView3.setLayoutParams(IVparams3);



        relativeLayout.addView(imageView1);
        relativeLayout.addView(imageView2);
        relativeLayout.addView(imageView3);
        R2Layout.addView(gallery);
        R2Layout.addView(relativeLayout);
        scrollView.addView(R2Layout);

        mImageIds=new ImageView[]{imageView1,imageView2,imageView3};
        mImageIds[0].setImageDrawable(getActivity().getResources().getDrawable(R.drawable.ic_dot_focused));
        ImgAdapter Gadapter=new ImgAdapter(getActivity());
        gallery.setAdapter(Gadapter);

    // Timer timer=new Timer();
    // timer.schedule(task, 2000,2000);
    // gallery.setOnItemSelectedListener(onItemSelectedListener);
        return scrollView;
    }

展开
收起
51干警网 2016-02-17 22:37:09 3961 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新
相关电子书
更多
蚂蚁聚宝Android秒级编译——Freeline
立即下载
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
相关镜像