如何在一个活动中展示一个正方形的颜色



我使用的是textView,设置它的背景颜色,但一旦我删除文本,它会缩小到没有,所以不能看到。

是否有一个控件,我可以用它来简单地显示一个方形调色板的颜色?

我的2个颜色方块-我想向用户显示从颜色选择器....中选择的当前颜色

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:id="@+id/textView_primaryColor"
        android:hint="Set Primary Color: Tap here"
        android:textColor="@color/white"
        android:layout_weight="1" />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textAppearance="@android:style/TextAppearance.Medium"
        android:id="@+id/textView_secondaryColor"
        android:hint="Set Secondary Color: Tap here"
        android:textColor="@color/white"
        android:layout_weight="1" />
</LinearLayout>
public void choosePrimaryColor(final Context context, int defaultColor) {
    AmbilWarnaDialog dialogColor = new AmbilWarnaDialog(context, defaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
        @Override
        public void onOk(AmbilWarnaDialog dialog, int color) {
            textView_primaryColor.setBackgroundColor(color);
            textView_primaryColor.setHint("");
            primaryColorChosen =  "#"+Integer.toHexString(color);
            Toast.makeText(context, "ok", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel(AmbilWarnaDialog dialog) {
            Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show();
        }
    });
    dialogColor.show();
}
public void chooseSecondaryColor(final Context context, int defaultColor) {
    final AmbilWarnaDialog dialogColor = new AmbilWarnaDialog(context, defaultColor, new AmbilWarnaDialog.OnAmbilWarnaListener() {
        @Override
        public void onOk(AmbilWarnaDialog dialog, int color) {
            textView_secondaryColor.setBackgroundColor(color);
            textView_secondaryColor.setHint("");
            secondaryColorChosen = "#"+Integer.toHexString(color);
            Toast.makeText(context, "ok", Toast.LENGTH_SHORT).show();
        }
        @Override
        public void onCancel(AmbilWarnaDialog dialog) {
            Toast.makeText(context, "cancel", Toast.LENGTH_SHORT).show();
        }
    });
    dialogColor.show();
}

看到你的评论,我认为你的意图是向用户显示他从颜色选择器中选择的颜色。

对于这个我的建议是添加一个新的布局(线性或相对),然后设置用户选择的颜色作为你刚刚创建的新布局的背景。

添加到您的xml

<LinearLayout
          android:id="@+id/layout_primaryColor"
          android:layout_height="100dp"     
          android:layout_width="100dp"/>

,然后在你的活动中添加

LinearLayout primaryColorLayout = (LinearLayout) findViewById(R.id.layout_primaryColor);
现在,要设置背景颜色,添加
primaryColorLayout .setBackgroundColor(Color.parseColor("#000000"));

最新更新