Android集成LinearLayout实现DoubleChoiceButton

         在写日记本时,想实现一个长按标签弹出黑色的Toggle(具体专业不知道怎么叫,就先这么叫吧大笑

      大概的样子如下

      

      在制作时发现文字始终不能正常显示,子布局没能填充父布局,这就让我很是惆怅。

      在网上搜寻答案,有说是需要在外部的LinearLayout设置Orientation,经试验,不行。

      后来看到有人遇到与我相同的问题,都是用了9.png图片做背景。起初并不具体9.png的规则是什么,只知道黑边是规定可拉伸部分的。

       原来做背景时,子控件只能填充黑边交叉的部分。

       终于解决了,就是下边那个尖尖有点失真了(欢迎补充怎么解决这个问题)

       下面附上源码:

       

package csu.scrovor.cn.inote.UI.Widgets.ChoiceButton;

import android.content.Context;
import android.graphics.Paint;
import android.graphics.Rect;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import csu.scrovor.cn.inote.R;

/**
 * Created by lenovo on 2016/10/29.
 */
public class ChoiceButton extends LinearLayout {
    private static final String REWRITE_TEXT = "修改";
    private static final String DELETE_TEXT = "删除";
    private TextView edit;
    private TextView delete;
    private ChoiceListener listener;
    private View content;

    public ChoiceButton(Context context) {
        super(context);
        initView(context);
    }

    public ChoiceButton(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChoiceButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }


    public void initView(Context context) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        content = inflater.inflate(R.layout.layout_choice_btn, null);
        edit = (TextView) content.findViewById(R.id.edit);
        delete = (TextView) content.findViewById(R.id.delete);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.addView(content, params);
    }


    public void setChoiceListener(final ChoiceListener listener) {
        this.listener = listener;
        edit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onChoiceItemOne();
            }
        });
        delete.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onChoiceItemTwo();
            }
        });
    }
}
   
Listener

/**
 * Created by lenovo on 2016/10/30.
 */
public interface ChoiceListener {
    public void onChoiceItemOne();
    public void onChoiceItemTwo();
}
xml

<?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="match_parent"
    android:orientation="horizontal"
    android:background="@drawable/ps">
    <android.support.v4.widget.Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>
    <TextView
        android:id="@+id/edit"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="6"
        android:text="Edit"
        android:gravity="center"
        android:textColor="#ffffff"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:text="|"
        android:textColor="#ffffff"/>
    <TextView
        android:id="@+id/delete"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:text="Delete"
        android:layout_weight="6"
        android:gravity="center"
        android:singleLine="true"
        android:textColor="#ffffff"/>

    <android.support.v4.widget.Space
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值