相关疑难解决方法(0)

Android:以编程方式添加两个文本视图

我试图以编程方式将视图添加到线性布局.

    LinearLayout layout     = (LinearLayout) findViewById(R.id.info);
    String [] informations  = topOffer.getInformations();
    TextView informationView;
    View line = new View(this);
    line.setLayoutParams(new LayoutParams(1, LayoutParams.FILL_PARENT));
    line.setBackgroundColor(R.color.solid_history_grey);
    for (int i = 0; i < informations.length; i++) {
        informationView = new TextView(this);
        informationView.setText(informations[i]);
        layout.addView(informationView, 0);
        layout.addView(line, 1);
    }
Run Code Online (Sandbox Code Playgroud)

首先,我只添加了informationsView,一切正常.在添加了line-View之后,它崩溃了,出现以下错误:

java.lang.IllegalStateException:指定的子级已有父级.您必须首先在孩子的父母上调用removeView().

所以我尝试了addView(View v,int index),但它崩溃了同样的消息......

有人有解决方案吗?

谢谢,马丁

java android android-linearlayout

12
推荐指数
2
解决办法
2万
查看次数

在RecyclerView OnCreateViewHolder中以编程方式创建或扩充视图

基本上我想创建一个布局或以编程方式用a夸大布局RecyclerView,但是无法这样做.我知道如何通过膨胀来处理xml,但出于好奇,我想以编程方式进行操作.

我的适配器代码如下:

public class ACShare extends RecyclerView.Adapter<ACShare.VHShare>{

    private List<Integer> listDrawalbe;

    public ACShare(Context context) {
        TypedArray drawables = context.getResources().obtainTypedArray(R.array.s_array_contact_us_share);
        listDrawalbe=new ArrayList<>();
        for (int i = 0; i <  drawables.length(); ++i)
            listDrawalbe.add( drawables.getResourceId(i, -1));
    }

    @Override
    public VHShare onCreateViewHolder(ViewGroup parent, int viewType) {
        return new VHShare(LayoutInflater.from(parent.getContext()).inflate(-1,parent,false));
    }

    @Override
    public void onBindViewHolder(VHShare holder, int position) {
        holder.imageView.setImageResource(listDrawalbe.get(position));
    }


    public class VHShare extends RecyclerView.ViewHolder
    {
        public ImageView imageView;
        public LinearLayout ll;

        public VHShare(View itemView) {
            super(itemView);

            Context context= GeneralFunction.getActivity(itemView);


            ll = new …
Run Code Online (Sandbox Code Playgroud)

android layout-inflater android-recyclerview

3
推荐指数
1
解决办法
7493
查看次数