如果兄弟姐妹不在,请查看填充空间

时间:2016-07-27 12:16:21

标签: android android-layout android-linearlayout relativelayout

所以,我TextView尝试使用ImageView来填充GONE LinearLayout时留下的空格......我已尝试使用{ {1}}和RelativeLayout,但ImageView的空间不是由TextView拍摄的。

那么,我是如何让它发挥作用的呢?

代码:

<RelativeLayout
    android:orientation="horizontal"
    android:id="@+id/cabecalho"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:id="@+id/imgPrimary"
        android:visibility="gone"
        android:src="@drawable/abc_btn_rating_star_on_mtrl_alpha"
        android:tint="#ecac31"
        android:layout_gravity="center_vertical"
        android:padding="3dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="17dp"
        android:text="Endereço de Teste"
        android:layout_toRightOf="@+id/imgPrimary"
        android:layout_toLeftOf="@+id/btnEdit"
        android:layout_centerVertical="true"
        android:id="@+id/addrTitle"
        android:layout_weight="50"
        android:textColor="@color/black"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dp" />

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/btnEdit"
        android:layout_weight="1"
        android:layout_toLeftOf="@+id/btnDelete"
        android:src="@drawable/icon_write_location"
        android:tint="#08b8ae"
        android:scaleType="fitCenter"
        android:background="@color/white"
        android:padding="10dp" />

    <ImageButton
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:id="@+id/btnDelete"
        android:layout_weight="1"
        android:scaleType="fitXY"
        android:src="@drawable/abc_ic_clear_mtrl_alpha"
        android:tint="#d60d0d"
        android:layout_alignParentRight="true"
        android:background="@color/white"
        android:padding="7dp" />
</RelativeLayout>

请求打印: The space is to the right of the bold "Teste" at the top of the white card. 空间位于大胆&#34; Teste&#34;的右边。在白卡的顶部。

来自我的recyclerview适配器的代码:

public class AddrAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private List<Address> addressList;

    public class ViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout layout;

        public TextView titulo, enderecoCompleto;
        public ImageButton btnEdit, btnDelete;
        public ImageView isDefaultStar;
        //public RelativeLayout cabecalho;

        public ViewHolder(LinearLayout layout) {
            super(layout);
            this.layout = layout;

            this.titulo = (TextView) layout.findViewById(R.id.addrTitle);
            this.enderecoCompleto = (TextView) layout.findViewById(R.id.addrComplete);
            this.btnEdit = (ImageButton) layout.findViewById(R.id.btnEdit);
            this.btnDelete = (ImageButton) layout.findViewById(R.id.btnDelete);
            this.isDefaultStar = (ImageView) layout.findViewById(R.id.imgPrimary);
            //this.cabecalho = (RelativeLayout) layout.findViewById(R.id.cabecalho);
        }
    }

    public class ViewHolderHint extends RecyclerView.ViewHolder {
        public TextView text;

        public ViewHolderHint(TextView text) {
            super(text);
            this.text = text;
        }
    }

    public AddrAdapter(List<Address> addresses) {
        this.addressList = addresses;
        if (addresses.size() > 0 && this.addressList.get(0).getCity() != null) {
            this.addressList.add(0, new Address());
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 0 : {
                TextView v = (TextView) LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.card_address_hint, parent, false);

                ViewHolderHint vhh = new ViewHolderHint(v);
                return vhh;
            }
            default : {
                LinearLayout v = (LinearLayout) LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.card_address, parent, false);

                ViewHolder vh = new ViewHolder(v);
                return vh;
            }
        }
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position < 1) {
            ViewHolderHint hint = (ViewHolderHint) holder;
            hint.text.setText(Html.fromHtml("Selecione um <b>endereço de entrega</b>"));
        } else {
            final ViewHolder layout = (ViewHolder) holder;

            layout.isDefaultStar.setVisibility((addressList.get(position).getDefaultAddress()) ? View.VISIBLE : View.GONE);
            //layout.cabecalho.requestLayout();
            layout.titulo.setText(addressList.get(position).getTitle());
            layout.enderecoCompleto.setText(
                    addressList.get(position).getDescription()+", "+
                            addressList.get(position).getNumber()+"\n"+
                            addressList.get(position).getComplement()+", "+
                            addressList.get(position).getNeighborhood()+" - "+
                            addressList.get(position).getCity().getName()+" - "+
                            addressList.get(position).getCity().getState().getCode()+"\n"+
                            addressList.get(position).getZipcode()
            );

            layout.btnEdit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = layout.getAdapterPosition();
                    Address addr = addressList.get(pos);

                    Bundle b = new Bundle();
                    b.putSerializable("ADDRESS_TO_EDIT",addr);
                    b.putInt("CHAVE_ENDERECO",pos); // TODO: Talvez no futuro seja a ID do endereço
                    Intent i = new Intent(AddressActivity.this,CheckoutAddressAddEditActivity.class);
                    i.putExtras(b);
                    startActivityForResult(i,006);
                }
            });

            layout.btnDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = layout.getAdapterPosition();

                    addresses.remove(pos);

                    notifyItemRemoved(pos);
                }
            });

            layout.layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = layout.getAdapterPosition();
                    Address addr = addressList.get(pos);

                    Bundle extras = new Bundle();
                    extras.putSerializable("ADDRESS", addr);
                    Intent data = new Intent();
                    data.putExtras(extras);
                    setResult(RESULT_OK, data);
                    finish();
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return addresses.size();
    }

    @Override
    public int getItemViewType(int pos) {
        return (pos > 0) ? 1 : 0;
    }
}

2 个答案:

答案 0 :(得分:0)

尝试使用线性布局,并为每个视图的布局和权重提供权重,并将每个视图的宽度设置为0dp,但确实为每个视图提供权重。在这种情况下,如果视图消失,则其他视图将占用其空间

答案 1 :(得分:0)

这里的诀窍实际上是不提供weightSum。

<LinearLayout
    android:weightSum=3
    ...>
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">  

将打印三个TextView,均匀间隔为每个可用空间的1/3,无论TextViews是否为View.GONE。

<LinearLayout
    ...>
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">
    <TextView
         android:layout_width="0dp"
         android:layout_weight="1">

将TextViews分隔为每个空间的1/3,直到其中一个为View.GONE,此时它将调整为每个1/2空间。