RatingBar变得半透明

时间:2017-06-02 11:19:04

标签: android android-view ratingbar

我在每行使用RecyclerView RatingBar。现在,有时RatingBar的部分内容被错误地绘制出来。离开屏幕并返回后,它们都恢复正常。我不知道为什么会发生这种情况,我甚至从RatingBar删除了任何样式,因此它应该具有默认外观。

它的外观如下:

enter image description here

在Nexus 6P(Android 7.1.1)上测试过。 还在三星Galaxy J3(2016)(Android 5.1.1)上测试过,这里没有问题。

我还添加了

holder.rbRating.requestLayout();
onBindViewHolder()中的

。它减少了一点问题,但它仍然存在。当我重新使用屏幕上的“坏”行时,它看起来很好。

这是行布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingTop="@dimen/main_margin_top"
    android:paddingBottom="@dimen/main_margin_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvRatingTitle"
        android:text="Czystość wody"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <RatingBar
        android:id="@+id/rbRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:isIndicator="true"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tvRatingTitle"
        android:numStars="5"
        android:rating="2.5"
        android:stepSize="0.1" />

    <CheckBox
        android:id="@+id/chkMissing"
        android:text="Zaznacz jeśli nie ma"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/rbRating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

我也尝试过切换到android.support.v7.widget.AppCompatRatingBar,但它没有任何区别。

编辑:

@Muhib Pirani解决方案似乎有效,但我有两个小问题:

1)在Nexus 6P上,第一层星星偏移了几个像素(放大查看):

enter image description here

2)在三星Galaxy J3(2016)上看起来像这样:

enter image description here

我对边框很好,但我希望它们在空星中也是绿色(不是灰色,背景应该是灰色的)。

2 个答案:

答案 0 :(得分:3)

我也遇到了同样的问题。我必须以编程方式将颜色设置为import java.awt.Color; import java.awt.EventQueue; import java.awt.Font; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.border.LineBorder; import com.alee.laf.WebLookAndFeel; import javax.swing.JTextField; import javax.swing.JButton; import net.sourceforge.jdatepicker.impl.JDatePickerImpl; import net.sourceforge.jdatepicker.impl.JDatePanelImpl; import net.sourceforge.jdatepicker.impl.UtilDateModel; import net.sourceforge.jdatepicker.util.JDatePickerUtil; import net.sourceforge.jdatepicker.impl.SqlDateModel; import net.sourceforge.jdatepicker.JDateComponentFactory; import net.sourceforge.jdatepicker.impl.UtilCalendarModel; import javax.swing.SpringLayout; import com.toedter.calendar.JDateChooser; public class JavaProject2_25 { private JFrame frame; private JTextField textField; private JTextField textField_1; private JTextField textField_2; private JTextField textField_3; private JTextField textField_4; private JTextField textField_5; private JTextField textField_6; private JTextField textField_7; private JPanel panel_1; private JPanel panel_2; private JPanel panel; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { JavaProject2_25 window = new JavaProject2_25(); window.frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the application. */ public JavaProject2_25() { initialize(); } public void getDataJavaProject2_25() { frame.setVisible(true); } /** * Initialize the contents of the frame. */ private void initialize() { try { UIManager.setLookAndFeel(new WebLookAndFeel()); } catch(Exception e) { e.printStackTrace(); } frame = new JFrame(); frame.setBounds(100, 100, 913, 839); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().setLayout(null); panel = new JPanel(); panel.setBounds(10, 11, 877, 94); panel.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); panel.setBackground(Color.decode("#35485E")); frame.getContentPane().add(panel); panel.setLayout(null); JLabel lblNewLabel_1 = new JLabel("Stock Entry"); lblNewLabel_1.setFont(new Font("Times New Roman", Font.BOLD, 20)); lblNewLabel_1.setForeground(Color.WHITE); lblNewLabel_1.setBounds(380, 34, 179, 22); panel.add(lblNewLabel_1); panel_1 = new JPanel(); panel_1.setBounds(10, 135, 521, 258); panel_1.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); frame.getContentPane().add(panel_1); panel_1.setLayout(null); panel_2 = new JPanel(); panel_2.setBounds(10, 11, 501, 46); panel_2.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); panel_2.setBackground(Color.decode("#35485E")); panel_1.add(panel_2); panel_2.setLayout(null); JLabel lblNewLabel = new JLabel("Supplier Info"); lblNewLabel.setForeground(Color.WHITE); lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 15)); lblNewLabel.setBounds(205, 17, 98, 14); panel_2.add(lblNewLabel); JLabel label = new JLabel("Product Code :"); label.setFont(new Font("Times New Roman", Font.PLAIN, 13)); label.setBounds(30, 86, 87, 16); panel_1.add(label); textField = new JTextField(); textField.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField.setColumns(10); textField.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField.setBounds(125, 80, 202, 28); panel_1.add(textField); JLabel lblDate = new JLabel("Date :"); lblDate.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblDate.setBounds(30, 130, 87, 16); panel_1.add(lblDate); JLabel lblSupplierId_1 = new JLabel("Supplier ID :"); lblSupplierId_1.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblSupplierId_1.setBounds(30, 173, 87, 16); panel_1.add(lblSupplierId_1); textField_1 = new JTextField(); textField_1.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_1.setColumns(10); textField_1.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_1.setBounds(125, 167, 202, 28); panel_1.add(textField_1); textField_2 = new JTextField(); textField_2.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_2.setColumns(10); textField_2.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_2.setBounds(125, 210, 202, 28); panel_1.add(textField_2); JLabel lblSupplierId = new JLabel("Supplier Name :"); lblSupplierId.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblSupplierId.setBounds(30, 216, 87, 16); panel_1.add(lblSupplierId); JButton btnEnter = new JButton("Enter"); btnEnter.setForeground(Color.BLACK); btnEnter.setFont(new Font("Times New Roman", Font.PLAIN, 13)); btnEnter.setBackground(new Color(39, 136, 215)); btnEnter.setBounds(371, 166, 89, 28); panel_1.add(btnEnter); JDateChooser dateChooser = new JDateChooser(); dateChooser.setBounds(125, 126, 202, 20); panel_1.add(dateChooser); JPanel panel_3 = new JPanel(); panel_3.setLayout(null); panel_3.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); panel_3.setBounds(10, 407, 521, 302); frame.getContentPane().add(panel_3); JPanel panel_4 = new JPanel(); panel_4.setLayout(null); panel_4.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); panel_4.setBackground(new Color(53, 72, 94)); panel_4.setBounds(10, 11, 501, 46); panel_3.add(panel_4); JLabel lblProductInfo = new JLabel("Product Info"); lblProductInfo.setForeground(Color.WHITE); lblProductInfo.setFont(new Font("Times New Roman", Font.BOLD, 15)); lblProductInfo.setBounds(205, 17, 98, 14); panel_4.add(lblProductInfo); JLabel label_2 = new JLabel("Product Code :"); label_2.setFont(new Font("Times New Roman", Font.PLAIN, 13)); label_2.setBounds(30, 86, 87, 16); panel_3.add(label_2); textField_3 = new JTextField(); textField_3.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_3.setColumns(10); textField_3.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_3.setBounds(125, 80, 202, 28); panel_3.add(textField_3); JLabel lblProductName = new JLabel("Product Name :"); lblProductName.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblProductName.setBounds(30, 130, 87, 16); panel_3.add(lblProductName); JLabel lblQuantity = new JLabel("Quantity :"); lblQuantity.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblQuantity.setBounds(30, 173, 87, 16); panel_3.add(lblQuantity); textField_4 = new JTextField(); textField_4.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_4.setColumns(10); textField_4.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_4.setBounds(125, 167, 202, 28); panel_3.add(textField_4); textField_5 = new JTextField(); textField_5.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_5.setColumns(10); textField_5.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_5.setBounds(125, 210, 202, 28); panel_3.add(textField_5); JLabel lblPricePerList = new JLabel("Price Per Unit :"); lblPricePerList.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblPricePerList.setBounds(30, 216, 87, 16); panel_3.add(lblPricePerList); JButton button = new JButton("Enter"); button.setForeground(Color.BLACK); button.setFont(new Font("Times New Roman", Font.PLAIN, 13)); button.setBackground(new Color(39, 136, 215)); button.setBounds(372, 80, 89, 28); panel_3.add(button); textField_6 = new JTextField(); textField_6.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_6.setColumns(10); textField_6.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_6.setBounds(125, 122, 202, 28); panel_3.add(textField_6); JLabel lblTotalAmount = new JLabel("Total Amount :"); lblTotalAmount.setFont(new Font("Times New Roman", Font.PLAIN, 13)); lblTotalAmount.setBounds(30, 259, 87, 16); panel_3.add(lblTotalAmount); textField_7 = new JTextField(); textField_7.setFont(new Font("Times New Roman", Font.PLAIN, 13)); textField_7.setColumns(10); textField_7.setBorder(new LineBorder(Color.decode("#0080AA"), 2, true)); textField_7.setBounds(125, 253, 202, 28); panel_3.add(textField_7); } } ,这样就可以了:

RatingBar

保持LayerDrawable layerDrawable = (LayerDrawable) ratingBar.getProgressDrawable(); layerDrawable.getDrawable(2).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP); layerDrawable.getDrawable(1).setColorFilter(ContextCompat.getColor(context, R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP); layerDrawable.getDrawable(0).setColorFilter(ContextCompat.getColor(context, R.color.editTextBor), PorterDuff.Mode.SRC_ATOP);//when not selected color. layerDrawable.getDrawable(2)相同的颜色。

答案 1 :(得分:1)

我也遇到了本机RatingBar的问题,所以我决定使用MaterialRatingBar。它解决了我所有的问题。

希望有所帮助