2

所以,我試圖讓一個TextView填補一個ImageView留下的空間時,它是GONE ......我用LinearLayoutRelativeLayout試過,但的ImageView空間ISN」 t由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. 空間是大膽「阿泰斯特」的在白牌的頂部右側。從我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; 
    } 
} 
+1

你可以分享你的屏幕,如果沒有太大的麻煩? – Shaishav

+0

關於你在說哪個imageView?首先,'imgPrimary'? –

+0

@SergeyGlotov是... –

回答

0

嘗試使用線性佈局和每個視圖提供weightsum佈局和重量,還可以設置每個視圖的寬度0dp但確實提供每個視圖的權重。在這種情況下,如果一個視圖不見了,它的空間將被另一個視圖所取代

+0

我試過了,如果你看我的代碼,你會看到有權重,我沒有刪除...即使imageview默認沒了,它的空間不佔用textview ... –

+0

你已經使用代碼中的相對佈局。您需要使用線性佈局,將weightSum參數設置爲每個視圖的佈局和layout_weight。也爲每個視圖設置寬度爲0 –

+0

像以前一樣,它沒有工作......我已經設置了weightSum和權重,但空間仍然是空白的......我檢查了Android設備監視器和ImageView真的沒有了,但它的空間沒有被佔用...... –

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"> 

將打印三個TextViews,均勻間隔爲每個可用空間的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的空間。