LinearLayout部分塗色問題

在主畫面做了基本的排版之後,最主要是讓每個星期都是一個直行的LinearLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/title"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:gravity="center_horizontal"
        android:text="課程表"
        android:textSize="18sp" />

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="新增"
        android:id="@+id/add"
        android:layout_gravity="right" />

    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        >
        <!-- 定义HorizontalScrollView,为里面的组件添加水平滚动条 -->
        <HorizontalScrollView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">"
                <LinearLayout
                    android:layout_width="20dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#00000000"
                        android:padding="5dp"
                        android:textColor="#00000000"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text=" "
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="1"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="2"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="3"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="4"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="5"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="6"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="7"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="8"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="9"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="10"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="11"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="12"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="13"/>
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="50dp"
                        android:textColor="#666666"
                        android:textSize="12sp"
                        android:gravity="center"
                        android:text="14"/>
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ll1"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="MON"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/ll2"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="TUE"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll3"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="WED"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll4"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="THU"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll5"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="FRI"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll6"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:padding="5dp"
                        android:textColor="#ffffff"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="STA"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
                <LinearLayout
                    android:id="@+id/ll7"
                    android:layout_width="100dp"
                    android:layout_height="wrap_content"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:paddingLeft="3dp"
                    android:paddingRight="3dp"
                    android:orientation="vertical">
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:background="#000000"
                        android:textColor="#ffffff"
                        android:padding="5dp"
                        android:textSize="15sp"
                        android:gravity="center"
                        android:text="SUN"
                        />
                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="2dp"
                        />
                </LinearLayout>
            </LinearLayout>
        </HorizontalScrollView>
    </ScrollView>
</LinearLayout>

主要是這樣子呈現


然後再用彈跳視窗去做到新增的功能

接著針對彈跳視窗所輸入的資料以及選擇的日期去做區域性的色塊處理 首先先處理好Spinner所被選取的資料,並且宣告一個int chooseday去接position的數字

adapter= new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,Day);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinner.setAdapter(adapter);


                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        switch (position){
                            case 0:
                                chooseday=position;

                                break;
                            case 1:
                                chooseday=position;

                                break;
                            case 2:
                                chooseday=position;

                                break;
                            case 3:
                                chooseday=position;

                                break;
                            case 4:
                                chooseday=position;

                                break;
                            case 5:
                                chooseday=position;

                                break;
                            case 6:
                                chooseday=position;

                                break;

並且寫一個setClass的方法,去處理畫面的塗色

private void setClass(LinearLayout ll,String title,String start,String end,int classes,int color){
        View view=LayoutInflater.from(this).inflate(R.layout.item,null);
        view.setMinimumHeight(dip2px(this,classes * 48));
        view.setBackgroundColor(colors[color]);
        ((TextView)view.findViewById(R.id.title)).setText(title);
        ((TextView)view.findViewById(R.id.place)).setText(start);
        ((TextView)view.findViewById(R.id.last)).setText(end);


        TextView blank1 = new TextView(this);
        TextView blank2 = new TextView(this);
        blank1.setHeight(dip2px(this,classes));
        blank2.setHeight(dip2px(this,classes));
        ll.addView(blank1);
        ll.addView(view);
        ll.addView(blank2);

    }

最後按下彈跳視窗裡的button之後,就可以完成LinearLayout的塗色

 if(chooseday==1){                          
 setClass(l1,et1.getText().toString(),et2.getText().toString(),et3.getText().toString(),2,3);
 }else {
 if(chooseday==2){
 setClass(l2,et1.getText().toString(),et2.getText().toString(),et3.getText().toString() ,2,1);

就完成了


完整程式碼 main

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private int colors[] = {
            Color.rgb(0xee,0xff,0xff),
            Color.rgb(0xf0,0x96,0x09),
            Color.rgb(0x8c,0xbf,0x26),
            Color.rgb(0x00,0xab,0xa9),
            Color.rgb(0x99,0x6c,0x33),
            Color.rgb(0x3b,0x92,0xbc),
            Color.rgb(0xd5,0x4d,0x34),
            Color.rgb(0xcc,0xcc,0xcc)
    };

    Button add;
    Context context;
    private String[] Day={"Sunday", "Monday", "Tuesday",
            "Wednesday", "Thursday", "Friday", "Saturday"};

    ArrayAdapter<String> adapter;
    int chooseday;
    LinearLayout ll1;
    List courseData[]=new ArrayList[7];
    int itemHeight;
    int marTop,marLeft;
    LinearLayout l1,l2,l3,l4,l5,l6,l7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        itemHeight=getResources().getDimensionPixelSize(R.dimen.weekItemHeight);
        marTop=getResources().getDimensionPixelSize(R.dimen.weekItemMarTop);
        marLeft=getResources().getDimensionPixelSize(R.dimen.weekItemMarLeft);



        add=(Button)findViewById(R.id.add);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               LayoutInflater layoutInflater=LayoutInflater.from(MainActivity.this);
                final View popupView=layoutInflater.inflate(R.layout.dialog,null);
                final AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
                builder.setView(popupView);
                 final EditText et1=(EditText)popupView.findViewById(R.id.editText);
                 final EditText et2=(EditText)popupView.findViewById(R.id.editText1);
                 final EditText et3=(EditText)popupView.findViewById(R.id.editText2);
                l1=(LinearLayout)findViewById(R.id.ll1);
                l2=(LinearLayout)findViewById(R.id.ll2);
                l3=(LinearLayout)findViewById(R.id.ll3);
                l4=(LinearLayout)findViewById(R.id.ll4);
                l5=(LinearLayout)findViewById(R.id.ll5);
                l6=(LinearLayout)findViewById(R.id.ll6);
                l7=(LinearLayout)findViewById(R.id.ll7);




                Button okbtn=(Button)popupView.findViewById(R.id.okbtn);

                Spinner spinner=(Spinner)popupView.findViewById(R.id.spinner);

                final AlertDialog dialog=builder.create();




                 adapter= new ArrayAdapter<String>(context,android.R.layout.simple_spinner_item,Day);
                adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinner.setAdapter(adapter);


                spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        switch (position){
                            case 0:
                                chooseday=position;


                                break;
                            case 1:
                                chooseday=position;

                                break;
                            case 2:
                                chooseday=position;

                                break;
                            case 3:
                                chooseday=position;

                                break;
                            case 4:
                                chooseday=position;

                                break;
                            case 5:
                                chooseday=position;

                                break;
                            case 6:
                                chooseday=position;

                                break;

                        }



                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {

                    }
                });

                okbtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(chooseday==1){
                            setClass(l1,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                    ,2,3);
                        }else {
                            if(chooseday==2){
                                setClass(l2,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                        ,2,1);
                            }else {
                                if(chooseday==3){
                                    setClass(l3,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                            ,2,3);
                                }else {
                                    if(chooseday==4){
                                        setClass(l4,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                                ,2,3);
                                    }else {
                                        if(chooseday==5){
                                            setClass(l5,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                                    ,2,3);
                                        }else {
                                            if(chooseday==6){
                                                setClass(l6,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                                        ,2,3);
                                            }else {
                                                if(chooseday==7){
                                                    setClass(l7,et1.getText().toString(),et2.getText().toString(),et3.getText().toString()
                                                            ,2,3);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        dialog.dismiss();
                        Toast.makeText(context,chooseday+et1.getText().toString()+et2.getText().toString()+et3.getText().toString()
                                ,Toast.LENGTH_LONG ).show();


                    }
                });
                dialog.show();



            }



        });


    }


    private void setClass(LinearLayout ll,String title,String start,String end,int classes,int color){
        View view=LayoutInflater.from(this).inflate(R.layout.item,null);
        view.setMinimumHeight(dip2px(this,classes * 48));
        view.setBackgroundColor(colors[color]);
        ((TextView)view.findViewById(R.id.title)).setText(title);
        ((TextView)view.findViewById(R.id.place)).setText(start);
        ((TextView)view.findViewById(R.id.last)).setText(end);


        TextView blank1 = new TextView(this);
        TextView blank2 = new TextView(this);
        blank1.setHeight(dip2px(this,classes));
        blank2.setHeight(dip2px(this,classes));
        ll.addView(blank1);
        ll.addView(view);
        ll.addView(blank2);

    }
    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);} /** * 根据手机的分辨率从 px(像素) 的单位 转成为 dp */
    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);}

    }

Dialog.Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="要新增的課程:"
        android:id="@+id/textView" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_weight="1" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="開始:"
            android:id="@+id/textView1" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText1"
            android:layout_weight="1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="結束:"
            android:id="@+id/textView2" />
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/editText2"
            android:layout_weight="1" />


    </LinearLayout>

results matching ""

    No results matching ""