如何动态改变可绘制的颜色和形状?

时间:2016-02-19 14:21:37

标签: android dynamic-programming background-color xml-drawable

我正在制作活动。我为事件创建了动态视图。

现在我想为我的活动设置颜色。我有drawable可以设置事件视图的形状。我希望在动态创建视图时更改视图的颜色。

我尝试通过setBackgroundColor()设置颜色; ,但颜色延伸了视图的形状。如何动态更改drawable的颜色?

事件视图xml布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/Parent">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shape"
        android:id="@+id/container"
        android:layout_marginLeft="60dp"
        android:layout_marginRight="12dp">

        <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
            android:id="@+id/textViewTitle"
            android:layout_marginLeft="10dp"
            android:textColor="@android:color/white"
            android:layout_marginTop="01dp"
            android:layout_alignParentStart="true" />

    </RelativeLayout>
</RelativeLayout> 

形状可绘制:

   <shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">


    <stroke android:width="2dip"/>

    <stroke android:color="@color/black" android:width="1dip"/>
    <corners android:radius="4dp"> </corners>

</shape>

Mon片段:

public class Mon extends Fragment {

    private FrameLayout fab;
    private EventTableHelper mDb;
    private Intent i;
    private ViewGroup dayplanView;
    private int minutesFrom,minutesTo;
    private List<EventData> events;
    private List<View> list;
    private EventData e;
    private LayoutInflater inflater;
    public  boolean editMode;
    private RelativeLayout container;
    RelativeLayout parent;
     View eventView;
    private boolean mCheckFragment;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mon, container, false);
        list = new ArrayList<View>();


        dayplanView = (ViewGroup) view.findViewById(R.id.hoursRelativeLayout);

        showEvents();


        mCheckFragment = true;


        return view;
    }

    private void createEvent(LayoutInflater inflater, ViewGroup dayplanView, int fromMinutes, int toMinutes, String title,String location,final int id,int color) {

       eventView = inflater.inflate(R.layout.event_view, dayplanView, false);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) eventView.getLayoutParams();

        container = (RelativeLayout) eventView.findViewById(R.id.container);
        TextView tvTitle = (TextView) eventView.findViewById(R.id.textViewTitle);
        list.add(eventView);

        eventView.setBackgroundColor(color);

        if (tvTitle.getParent() != null)
           ((ViewGroup) tvTitle.getParent()).removeView(tvTitle);

        if(location.equals(""))
        {
            tvTitle.setText("Event : " + title);
        }
        else
        {
            tvTitle.setText("Event : " + title + " (At : " + location +")");
        }
        int distance = (toMinutes - fromMinutes);
        layoutParams.topMargin = dpToPixels(fromMinutes + 9);
        layoutParams.height = dpToPixels(distance);

        eventView.setLayoutParams(layoutParams);
        dayplanView.addView(eventView);
        container.addView(tvTitle);



        eventView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                i = new Intent(getActivity(), AddEventActivity.class);
                editMode = true;
                i.putExtra("EditMode", editMode);
                i.putExtra("id", id);
                startActivityForResult(i, 1);

            }
        });
    }

    public void showEvents()
    {
        mDb = new EventTableHelper(getActivity());
        events = mDb.getAllEvents("Mon");

        for (EventData eventData : events) {

            int id = eventData.getId();
            String datefrom = eventData.getFromDate();

            if (datefrom != null) {
                String[] times = datefrom.substring(11, 16).split(":");
                minutesFrom = Integer.parseInt(times[0]) * 60 + Integer.parseInt(times[1]);
            }
            String title = eventData.getTitle();
            String location = eventData.getLocation();
            String dateTo = eventData.getToDate();
            int color = eventData.getColor();


            if (dateTo != null) {
                //times = dateTo.substring(11,16).split(":");
                String[] times1 = dateTo.substring(11, 16).split(":");
                minutesTo = Integer.parseInt(times1[0]) * 60 + Integer.parseInt(times1[1]);
            }
            createEvent(inflater, dayplanView, minutesFrom, minutesTo, title, location, id, color);
            id++;


        }
    }

    public void removeView()
    {
        for(int i=0; i<list.size(); i++)
        {
            View view = (View)list.get(i);
            dayplanView.removeView(view);
        }
    }

    private int dpToPixels(int dp) {
        return (int) (dp * getResources().getDisplayMetrics().density);
    }

    @Override
    public void onResume()
    {

        super.onResume();

        if(mCheckFragment)
        {
            removeView();
            showEvents();

        }

    }

}

看起来像这样: enter image description here 谢谢..

1 个答案:

答案 0 :(得分:1)

您可以删除第一个相对布局并修改此行eventView.setBackgroundColor(color);,如下所示:

((GradientDrawable)eventView.getBackground()).setColor(color);
相关问题