android如何寫一個投票或是表達觀點的介面,android觀點

來源:互聯網
上載者:User

android如何寫一個投票或是表達觀點的介面,android觀點

先:




把這些表示觀點的view放在一個LinearLayout裡:

<LinearLayout        xmlns:android="http://schemas.android.com/apk/res/android"        android:id="@+id/repost_vote_tag_list"           android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:orientation="vertical"          android:visibility="gone">      </LinearLayout>

每個Item可以這樣來實現:

public class KXTagWidget extends TextView{String mTagName = "";/** colors */int mBgColor = Color.WHITE;int mPressedBgColor = Color.WHITE;int mTextColor = Color.WHITE;int mPressedTextColor = Color.BLACK;public KXTagWidget(Context context) {super(context);setClickable(true);}public KXTagWidget(Context context, AttributeSet attrs) {super(context, attrs);setClickable(true);}public KXTagWidget(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);setClickable(true);}public void setName(String tagName){if(tagName != null){mTagName = tagName;this.setText(mTagName);}}public void setBgColor(int bgColor, int pressedBgColor){mBgColor = bgColor;mPressedBgColor = pressedBgColor;this.setBackgroundColor(bgColor);//this.set}public int getmPressedBgColor() {return mPressedBgColor;}public void setmPressedBgColor(int mPressedBgColor) {this.mPressedBgColor = mPressedBgColor;}public int getmPressedTextColor() {return mPressedTextColor;}public void setmPressedTextColor(int mPressedTextColor) {this.mPressedTextColor = mPressedTextColor;}public int getmBgColor() {return mBgColor;}public void setmBgColor(int mBgColor) {this.mBgColor = mBgColor;}public int getmTextColor() {return mTextColor;}public void setmTextColor(int mTextColor) {this.mTextColor = mTextColor;}public void setTextColor(int textColor, int pressedTextColor){mTextColor = textColor;mPressedTextColor = pressedTextColor;}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//加畫邊框Paint paint = new Paint();paint.setColor(this.mBgColor);int width = getWidth();int height = getHeight();canvas.drawLine(0, 0, width - 1, 0, paint);canvas.drawLine(width - 1, 0, width - 1, height , paint);canvas.drawLine(width - 1, height -1, 0, height - 1, paint);canvas.drawLine(0, height -1 , 0, 0, paint);}public int getMesuredWidth() {int mesuredWidth = 0;Paint p = this.getPaint();float textWidth = p.measureText(mTagName);mesuredWidth = (int)textWidth + this.getCompoundPaddingLeft()+ this.getCompoundPaddingRight();return mesuredWidth;}}

Activity的實現:

private static final int VOTE_BAR_IMAGE_NUM = 9;private int mVoteBarImage[] = new int[VOTE_BAR_IMAGE_NUM];private HashMap<String, View> mVoteControlMap = new HashMap<String, View>();private int mTagBgColor[] = new int[14];/** tag touch listener */private TagOnTouchListener mTagOnTouchListener = null;/** tag click listener */private TagOnClickListener mTagOnClickListener = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initTagBgColor();mTagList.add("導致反對微軟");mTagList.add("聲浪再次掀起的");mTagList.add("聲勢");mTagList.add("XXX");mTagList.add("YYYYYY");mTagList.add("XXXXXXXXXXXXXX");mTagList.add("ZZZZZZZZ");mTagList.add("其中一些中小規模的公司甚至經常低於國家標準甚至不給賠償");mTagList.add("按照國家勞動法有關規定");mTagList.add("即以在諾基亞公司");mTagList.add("國家");constructViews();}private void constructViews() {// total number of votesString text = "一共有"+String.valueOf(11)+"項";TextView view = (TextView) this.findViewById(R.id.repost_vote_total_num_des);view.setText(text);// friend's opinion labelTextView view2 = (TextView) this.findViewById(R.id.repost_vote_friend_opinion_label);View answerlistView = this.findViewById(R.id.repost_vote_anwser_list);View taglistView = this.findViewById(R.id.repost_vote_tag_list);View inputView = this.findViewById(R.id.repost_vote_input_layout);answerlistView.setVisibility(View.GONE);taglistView.setVisibility(View.VISIBLE);inputView.setVisibility(View.VISIBLE);// tag list viewconstructTagListView();// result list view// constructResultListView();}private void initTagBgColor() {int i;for (i = 0; i < 7; ++i) {int r = 231 - i * 18;int g = 85 - i * 3;int b = 85 - i * 3;mTagBgColor[i] = Color.rgb(r, g, b);}for (i = 7; i < 14; ++i) {int c = 118 + (i - 7) * 14;mTagBgColor[i] = Color.rgb(c, c, c);}}private ArrayList<String> mTagList = new ArrayList<String>();private void constructTagListView() {LinearLayout parent = (LinearLayout) findViewById(R.id.repost_vote_tag_list);if (mTagList == null) {parent.setVisibility(View.GONE);return;}if (mTagList.size() == 0) {parent.setVisibility(View.GONE);return;}parent.removeAllViews();int screenWidth = getWindowManager().getDefaultDisplay().getWidth();float totalWidth = screenWidth - 25.0f;int tagTotalNum = Math.min(mTagList.size(), 15);/** 可放TAG的剩餘空間 */float surplus = totalWidth;LinearLayout oneLine = null;oneLine = new LinearLayout(this);LayoutParams params = new LayoutParams();params.width = LayoutParams.FILL_PARENT;params.height = LayoutParams.WRAP_CONTENT;params.bottomMargin = 1;oneLine.setLayoutParams(params);String tagName = "";for (int i = 0; i < tagTotalNum; i++) {KXTagWidget oneTag = (KXTagWidget) getLayoutInflater().inflate(R.layout.kx_widget_view, null);tagName = mTagList.get(i);oneTag.setName(tagName);oneTag.setTag(i);oneTag.setBgColor(selectTagBgColor(i), Color.WHITE);oneTag.setOnClickListener(this.mTagOnClickListener);this.mTagOnClickListener = new TagOnClickListener();this.mTagOnTouchListener = new TagOnTouchListener();oneTag.setOnTouchListener(this.mTagOnTouchListener);oneTag.setOnClickListener(mTagOnClickListener);float viewWidth = oneTag.getMesuredWidth();if (surplus > viewWidth) {// 如果surplus剩餘可畫空間能容得下oneTag,則在該行放下這個tag並重新計算剩餘可畫空間surplusoneLine.addView(oneTag);surplus -= viewWidth;} else {// 如果容不下就換一行,並把上一行add進去,初始化surplusparent.addView(oneLine);surplus = totalWidth;oneLine = new LinearLayout(this);oneLine.setLayoutParams(params);oneLine.addView(oneTag);surplus -= viewWidth;}}if (null != oneLine) {parent.addView(oneLine);}}private int selectTagBgColor(int index) {if (index < mTagBgColor.length) {return mTagBgColor[index];} else {return mTagBgColor[mTagBgColor.length - 1];}}private int selectVoteBarImage(int index) {int i = index % VOTE_BAR_IMAGE_NUM;return mVoteBarImage[i];}private void initVoteBarImage() {mVoteBarImage[0] = R.drawable.votec1;mVoteBarImage[1] = R.drawable.votec2;mVoteBarImage[2] = R.drawable.votec3;mVoteBarImage[3] = R.drawable.votec4;mVoteBarImage[4] = R.drawable.votec5;mVoteBarImage[5] = R.drawable.votec6;mVoteBarImage[6] = R.drawable.votec7;mVoteBarImage[7] = R.drawable.votec8;mVoteBarImage[8] = R.drawable.votec9;}private class TagOnTouchListener implements OnTouchListener {@Overridepublic boolean onTouch(View v, MotionEvent event) {KXTagWidget tagView = (KXTagWidget) v;switch (event.getAction()) {case MotionEvent.ACTION_DOWN: {CharSequence tagName = tagView.getText();tagView.setTextColor(tagView.getmPressedTextColor());tagView.setBackgroundColor(tagView.getmPressedBgColor());}break;case MotionEvent.ACTION_UP:case MotionEvent.ACTION_OUTSIDE:case MotionEvent.ACTION_CANCEL: {tagView.setTextColor(tagView.getmTextColor());tagView.setBackgroundColor(tagView.getmBgColor());}break;default:break;}return false;}}private class TagOnClickListener implements OnClickListener {@Overridepublic void onClick(View view) {Integer tagIndex = (Integer) view.getTag();String name = mTagList.get(tagIndex);Toast.makeText(MainActivity.this, name, Toast.LENGTH_LONG).show();}}


代碼在http://download.csdn.net/detail/baidu_nod/7711097下載


android 怎寫一個類似menu的介面

final PopupWindow p = new PopupWindow(this);
p.setContentView(preview);
p.setWidth((int) (sWidth * count + extraW));
p.setHeight((int) (sHeight + extraH));
p.setAnimationStyle(R.style.AnimationPreview);
p.setOutsideTouchable(true);
p.setFocusable(true);
p.setBackgroundDrawable(new ColorDrawable(0));
p.showAsDropDown(anchor, 0, 0);

p.setOnDismissListener(new PopupWindow.OnDismissListener() {
public void onDismiss() {
dismissPreview(anchor);
}
});
 
android這個介面怎寫,或者怎布局

<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="80"
>

<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="90"
android:background="#FF0000"
>

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textColor="#ffffff"
android:text="資料顯示" />

</RelativeLayout>

<TableLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_......餘下全文>>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.