如下图所示,按钮和文本视图之间存在间隙。我需要删除此空间或我需要将其颜色更改为原色。这是我的代码和快照
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="5">
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="2"
android:orientation="vertical"
android:background="@color/colorPrimary"
android:paddingRight="2dp"
android:paddingLeft="2dp"
android:layout_height="170dp">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/splash"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/logo1"
android:layout_width="fill_parent"
android:layout_height="100sp"
android:orientation="horizontal"
android:layout_weight="1"
android:padding="2dp"
android:background="@color/colorPrimary">
<LinearLayout
android:id="@+id/mobileRelView"
android:orientation="vertical"
android:weightSum="2"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="1dp"
android:onClick="goToMobileForm"
android:layout_weight="0.25">
<ImageView
android:padding="7dp"
android:id="@+id/mobile_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="1dp"
android:clickable="false"
android:layout_weight="0.7"
android:scaleType="fitCenter"
android:src="@drawable/smartphone"/>
<Button
android:id="@+id/mobile_button"
android:layout_width="match_parent"
android:layout_weight="1.3"
android:clickable="false"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
android:text="Mobile"
android:textAllCaps="false"
android:textColor="#000000"
android:textSize="@dimen/mainbuttontext"/>
<View
android:layout_width="70dp"
android:layout_height="2dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:background="#424242" />
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#424242" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:onClick="goToDesktopForm"
android:weightSum="2"
android:clickable="true"
android:paddingLeft="2dp"
android:paddingRight="1dp"
android:layout_weight="0.25">
<ImageView
android:id="@+id/desktop_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:clickable="false"
android:scaleType="fitCenter"
android:src="@drawable/compuericon"/>
<Button
android:text="Desktop"
android:textAllCaps="false"
android:layout_width="match_parent"
android:clickable="false"
android:layout_height="match_parent"
android:id="@+id/desktop_button"
android:background="@color/colorPrimary"
android:textSize="@dimen/mainbuttontext"
android:textColor="#000000"
android:layout_weight="1.3" />
<View
android:layout_width="70dp"
android:layout_height="2dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:background="#424242" />
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#424242" />
<LinearLayout
android:layout_width="match_parent"
android:paddingLeft="1dp"
android:orientation="vertical"
android:onClick="goToLaptopForm"
android:weightSum="2"
android:layout_height="match_parent"
android:layout_weight="0.25">
<ImageView
android:id="@+id/laptop_Image"
android:layout_width="match_parent"
android:layout_weight="0.7"
android:clickable="false"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/laptop" />
<Button
android:text="Laptop"
android:textAllCaps="false"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/laptop_button"
android:textSize="@dimen/mainbuttontext"
android:background="@color/colorPrimary"
android:textColorHint="#ffffff"
android:layout_weight="1.3"
android:textColor="#000000"/>
<View
android:layout_width="70dp"
android:layout_height="2dp"
android:layout_alignParentRight="true"
android:layout_gravity="center"
android:background="#424242" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/logobutton1"
android:layout_width="fill_parent"
android:layout_height="100sp"
android:orientation="horizontal"
android:layout_weight="1"
android:padding="2dp"
android:background="@color/colorPrimary">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="2dp"
android:paddingLeft="1dp"
android:onClick="gotToOthersForm"
android:weightSum="2"
android:layout_weight="0.25">
<ImageView
android:id="@+id/homeappliances_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.7"
android:padding="7dp"
android:clickable="false"
android:scaleType="fitCenter"
android:src="@drawable/washingmachine"/>
<Button
android:text="Home Appliance"
android:textAllCaps="false"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/homeappliances_button"
android:background="@color/colorPrimary"
android:textSize="@dimen/mainbuttontext"
android:layout_weight="1.3"
android:textColor="#000000"/>
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#424242" />
<LinearLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="1dp"
android:weightSum="2"
android:onClick="goToDesktopForm"
android:layout_weight="0.25">
<ImageView
android:layout_weight="0.7"
android:clickable="false"
android:id="@+id/cleaning_Image"
android:padding="7dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/caricon" />
<Button
android:text="Automobile"
android:textAllCaps="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/cleaning_button"
android:background="@color/colorPrimary"
android:textSize="@dimen/mainbuttontext"
android:layout_weight="1.3"
android:clickable="false"
android:textColor="#000000" />
</LinearLayout>
<View
android:layout_width="2dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#424242" />
<LinearLayout
android:orientation="vertical"
android:weightSum="2"
android:onClick="gotToOthersForm"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="1dp"
android:layout_weight="0.25">
<ImageView
android:layout_weight="0.7"
android:id="@+id/othrs_Image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:padding="7dp"
android:clickable="false"
android:src="@drawable/othersicon" />
<Button
android:text="Others"
android:textAllCaps="false"
android:clickable="false"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/others_button"
android:background="@color/colorPrimary"
android:textSize="@dimen/mainbuttontext"
android:layout_weight="1.3"
android:textColor="#000000"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="150sp">
<!--<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#6A1B9A"
android:textStyle="bold"
android:background="#ffb300"
android:textSize="15sp"
android:text="Why eFix ?" />
-->
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="2dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/abca" />
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
答案 0 :(得分:0)
由于使用了public class ProductAdapter extends ArrayAdapter<Product> {
ArrayList<Boolean> positionArray;
public ProductAdapter(Context context, ArrayList<Product> products){
super(context,0,products);
positionArray = new ArrayList<Boolean>(products.size());
for(int i =0;i<products.size();i++){
positionArray.add(false);
}
}
public static class ViewHolder{
TextView tv_price,tv_prodName, tv_details, tv_location, quantity;
ImageView imgView_productImage;
CheckBox checkBox;
Button bntUp,btnDown;
}
//When a listview recycles views , it recycles its present state as well as listeners attached to it.if the checkbox was checked and has a
// onCheckedChangeListener set, both will remain a part of recycled view based on position.
// So it is our responsibility to reset all states and remove previous listeners.
@NonNull
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
Product product = getItem(position);
if(convertView==null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_product_item,parent, false);
viewHolder = new ViewHolder();
viewHolder.imgView_productImage = (ImageView)convertView.findViewById(R.id.list_item_imgView_productImage);
viewHolder.tv_price = (TextView)convertView.findViewById(R.id.listItem_tv_price);
viewHolder.tv_prodName = (TextView)convertView.findViewById(R.id.listItem_tv_productName);
viewHolder.tv_location = (TextView)convertView.findViewById(R.id.listItem_tv_location);
viewHolder.tv_details = (TextView)convertView.findViewById(R.id.listItem_tv_productDetail);
viewHolder.checkBox = (CheckBox)convertView.findViewById(R.id.listProd_checkbox);
viewHolder.bntUp = (Button)convertView.findViewById(R.id.listProd_up);
viewHolder.btnDown = (Button)convertView.findViewById(R.id.listProd_down);
viewHolder.quantity = (TextView)convertView.findViewById(R.id.listItem_quantity);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.checkBox.setOnCheckedChangeListener(null); //
}
viewHolder.tv_price.setText("$" + String.valueOf(product.getPrice()));
viewHolder.tv_prodName.setText(product.getProductName());
viewHolder.tv_location.setText(product.getLocation());
viewHolder.tv_details.setText(product.getProductDetail());
viewHolder.imgView_productImage.setImageBitmap(MifareUtilities.getDecodeImage(product.getProductImage()));
viewHolder.checkBox.setChecked(positionArray.get(position));
int visible = positionArray.get(position) == true ? View.VISIBLE : View.GONE;
viewHolder.checkBox.setVisibility(visible);
viewHolder.bntUp.setVisibility(visible);
viewHolder.btnDown.setVisibility(visible);
viewHolder.quantity.setVisibility(visible);
viewHolder.quantity.setText(String.valueOf(product.getQuantity()));
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
positionArray.set(position,true);
//show buttons
changeVisibility(viewHolder,View.VISIBLE);
}else {
positionArray.set(position,false);
changeVisibility(viewHolder,View.GONE);
}
}
});
return convertView;
}
public void changeVisibility(ViewHolder viewHolder, int visibility){
viewHolder.bntUp.setVisibility(visibility);
viewHolder.btnDown.setVisibility(visibility);
viewHolder.quantity.setVisibility(visibility);
viewHolder.checkBox.setVisibility(visibility);
}
}
,这种情况正在发生。使用Button
代替TextView
。
#。我修改了您的XML并删除了一些冗余属性,并使用正确的Button
和ImageView
更新了TextView
和weight
。
#。我已将属性height
添加到android:background="?attr/selectableItemBackground"
,以便在点击时显示LinearLayout
效果。
以下是工作代码:
ripple
<强>输出:强>