Exemplo n.º 1
0
 /**
  * 从xml获取动态color
  *
  * @param context
  * @param colorid
  * @return
  */
 public static ColorStateList getXMLColor(Context context, int colorid) {
   XmlResourceParser xrp = context.getResources().getXml(colorid);
   try {
     ColorStateList csl = ColorStateList.createFromXml(context.getResources(), xrp);
     return csl;
   } catch (Exception e) {
   }
   return null;
 }
  public MainGroupAdapter(Context context, List<MainGroup> objects) {
    super(context);
    clearAndSetObject(objects);

    try {
      mBlackColorStateList =
          ColorStateList.createFromXml(
              mContext.getResources(),
              mContext.getResources().getXml(R.color.textcolor_black_white));
    } catch (NotFoundException e) {
    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }
  }
Exemplo n.º 3
0
  public void testColorStateList() throws NotFoundException, XmlPullParserException, IOException {
    final int[][] state = new int[][] {{0}, {0}};
    final int[] colors = new int[] {Color.RED, Color.BLUE};
    ColorStateList c = new ColorStateList(state, colors);
    assertTrue(c.isStateful());
    assertEquals(Color.RED, c.getDefaultColor());

    final int alpha = 36;
    final ColorStateList c1 = c.withAlpha(alpha);
    assertNotSame(Color.RED, c1.getDefaultColor());
    // check alpha
    assertEquals(alpha, c1.getDefaultColor() >>> 24);
    assertEquals(Color.RED & 0x00FF0000, c1.getDefaultColor() & 0x00FF0000);

    final int xmlId = R.drawable.testcolor;
    final int colorInXml = 0xFFA6C839; // this color value is define in testcolor.xml file.
    final Resources res = getContext().getResources();
    c = ColorStateList.createFromXml(res, res.getXml(xmlId));
    assertEquals(colorInXml, c.getDefaultColor());
    assertEquals(0, c.describeContents());
    assertFalse(c.isStateful());
    assertNotNull(c.toString());
    assertEquals(colorInXml, c.getColorForState(new int[] {0}, 0));

    c = ColorStateList.valueOf(Color.GRAY);
    assertEquals(Color.GRAY, c.getDefaultColor());

    final Parcel parcel = Parcel.obtain();
    c.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    ColorStateList actual = ColorStateList.CREATOR.createFromParcel(parcel);
    // can only compare the state and the default color. because no API to
    // get every color of ColorStateList
    assertEquals(c.isStateful(), actual.isStateful());
    assertEquals(c.getDefaultColor(), actual.getDefaultColor());
  }
  private void initMainGroupCellView(Context context) {
    setOrientation(LinearLayout.VERTICAL);

    mCell = new RelativeLayout(context);
    mCell.setBackgroundResource(R.drawable.list_selector);

    mWrapper = new LinearLayout(context);
    mWrapper.setOrientation(LinearLayout.HORIZONTAL);
    mWrapper.setGravity(Gravity.CENTER_VERTICAL);

    mSectionTextView = new TextView(context);
    mSectionTextView.setTextColor(context.getResources().getColor(R.color.header_section));
    mSectionTextView.setGravity(Gravity.CENTER);
    mSectionTextView.setTypeface(null, Typeface.BOLD);
    mSectionTextView.setBackgroundColor(
        Color.WHITE); // setBackgroundResource(R.drawable.bg_section_header);
    addView(mSectionTextView);
    mSectionTextView.getLayoutParams().width = LinearLayout.LayoutParams.MATCH_PARENT;

    mImageView = new ImageView(context);
    mImageView.setId(R.id.imageView1);
    mLearningStatisticImageView = new ImageView(context);
    mLearningStatisticImageView.setId(R.id.imageView4);
    mLearningStatisticImageView.setVisibility(View.GONE);

    mTextView = new TextView(context);
    try {
      mTextView.setTextColor(
          ColorStateList.createFromXml(
              getResources(), getResources().getXml(R.color.textcolor_black_white)));
    } catch (NotFoundException e) {
    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }
    mTextView.setTextSize(14.0f);
    mTextView.setTypeface(null, Typeface.BOLD);
    mTextView.setGravity(Gravity.CENTER_VERTICAL);

    mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleHorizontal);
    try {
      mProgressBar.setProgressDrawable(
          Drawable.createFromXml(
              getResources(), getResources().getXml(R.drawable.learning_progress_bar)));
    } catch (NotFoundException e) {
    } catch (XmlPullParserException e) {
    } catch (IOException e) {
    }
    mProgressBar.setId(android.R.id.progress);
    mProgressBar.setMax(100);

    mScale = context.getResources().getDisplayMetrics().density;

    LinearLayout.LayoutParams linearParams =
        new LinearLayout.LayoutParams((int) (30 * mScale), (int) (30 * mScale));
    linearParams.rightMargin = (int) (20 * mScale);
    mWrapper.addView(mImageView, linearParams);

    linearParams =
        new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    mWrapper.addView(mTextView, linearParams);

    RelativeLayout.LayoutParams layoutParams =
        new RelativeLayout.LayoutParams((int) (70 * mScale), (int) (20 * mScale));
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    layoutParams.rightMargin = (int) (10 * mScale);
    mCell.addView(mProgressBar, layoutParams);

    layoutParams =
        new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
    layoutParams.rightMargin = (int) (10 * mScale);
    mCell.addView(mLearningStatisticImageView, layoutParams);

    layoutParams =
        new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    layoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
    layoutParams.addRule(RelativeLayout.LEFT_OF, mProgressBar.getId());
    layoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
    layoutParams.leftMargin = (int) (6 * mScale);
    layoutParams.rightMargin = (int) (10 * mScale);
    mCell.addView(mWrapper, layoutParams);

    addView(
        mCell,
        new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
  }