SettingView 代码
/**
* Created by lwm on 2020/5/16
*/
public class SettingView extends FrameLayout {
private Context mContext;
private String left_text_default = "";
private String right_text_default = "";
private int left_size_default = 15;
private int right_size_default = 14;
private int left_color_default = Color.parseColor("#4d4d4d");
private int right_color_default = Color.parseColor("#999999");
private int right_image_visible_default = VISIBLE;
private int left_image_visible_default = GONE;
private Drawable right_image_src_default;
private Drawable left_image_src_default;
public TextView settingLeftText;
public TextView settingRightText;
public ImageView settingRightIcon;
public ImageView settingLeftIcon;
public View view;
public SettingView(@NonNull Context context) {
super(context);
initView(context, null);
}
public SettingView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
public SettingView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
public void initView(Context mContext, AttributeSet attrs) {
this.mContext = mContext;
view = LayoutInflater.from(this.getContext()).inflate(R.layout.view_setting, this, true);
settingLeftText = findViewById(R.id.setting_left_text);
settingRightText = findViewById(R.id.right_text);
settingRightIcon = findViewById(R.id.setting_right_icon);
settingLeftIcon = findViewById(R.id.left_image);
right_image_src_default = getResources().getDrawable(R.mipmap.ic_right_arrow_gray);
left_image_src_default = getResources().getDrawable(R.mipmap.ic_right_arrow_gray);
TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.SettingView);
if (typedArray != null) {
String left_text = typedArray.getString(R.styleable.SettingView_left_text_content);
String right_text = typedArray.getString(R.styleable.SettingView_right_text_content);
left_color_default = typedArray.getInt(R.styleable.SettingView_left_text_color, left_color_default);
left_size_default = typedArray.getInt(R.styleable.SettingView_left_text_size, left_size_default);
right_color_default = typedArray.getInt(R.styleable.SettingView_right_text_color, right_color_default);
right_size_default = typedArray.getInt(R.styleable.SettingView_right_text_size, right_size_default);
Drawable imageSrc_right = typedArray.getDrawable(R.styleable.SettingView_right_image_src);
Drawable imageSrc_left = typedArray.getDrawable(R.styleable.SettingView_left_image_src);
right_image_visible_default = typedArray.getInt(R.styleable.SettingView_right_image_visible, right_image_visible_default);
left_image_visible_default = typedArray.getInt(R.styleable.SettingView_left_image_visible, left_image_visible_default);
if (left_text != null) {
left_text_default = left_text;
}
if (right_text != null) {
right_text_default = right_text;
}
if (imageSrc_right != null) {
right_image_src_default = imageSrc_right;
}
if (imageSrc_left != null) {
left_image_src_default = imageSrc_left;
left_image_visible_default= VISIBLE;
}else{
left_image_visible_default=GONE;
}
}
settingLeftText.setText(left_text_default);
settingLeftText.setTextSize(left_size_default);
settingLeftText.setTextColor(left_color_default);
settingRightText.setText(right_text_default);
settingRightText.setTextSize(right_size_default);
settingRightText.setTextColor(right_color_default);
settingRightIcon.setImageDrawable(right_image_src_default);
settingRightIcon.setVisibility(right_image_visible_default);
settingLeftIcon.setVisibility(left_image_visible_default);
settingLeftIcon.setImageDrawable(left_image_src_default);
}
}
ToolUtils 代码(实现复制功能的代码)
具体复制功能的实现可以看:https://blog.csdn.net/weixin_42814000/article/details/106068474
/**
* Created by lwm on 2020/5/16
*/
public class ToolUtils {
/**
* 复制String
*
* @param context
* @param text
*/
public static void copyStringText(Context context, String text) {
ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("simple text", text);
clipboard.setPrimaryClip(clip);
Toast.makeText(context,"复制成功",Toast.LENGTH_SHORT).show();
}
}
MainActivity 代码
public class MainActivity extends AppCompatActivity {
private SettingView namesv;
private SettingView lettersv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
namesv = (SettingView) findViewById(R.id.name_sv);
lettersv = (SettingView) findViewById(R.id.letter_sv);
namesv.settingRightText.setWidth(400);
namesv.settingRightText.setText("林伟茂");
lettersv.settingRightText.setWidth(400);
lettersv.settingRightText.setText("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
namesv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ToolUtils.copyStringText(MainActivity.this, namesv.settingRightText.getText().toString());
}
});
}
}
activity_main.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<com.lwm.settingviewdemo.SettingView
android:id="@+id/name_sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:left_text_content="@string/name"
app:right_image_src="@mipmap/ic_help_right"/>
<View style="@style/style_line"></View>
<com.lwm.settingviewdemo.SettingView
android:id="@+id/letter_sv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:left_text_content="@string/letter" />
</LinearLayout>
view_setting.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:background="@color/c_ffffff"
android:paddingHorizontal="@dimen/dp_16"
android:layout_height="@dimen/dp_48">
<ImageView
android:id="@+id/left_image"
android:layout_marginRight="@dimen/dp_15"
android:layout_width="@dimen/dp_18"
android:layout_height="@dimen/dp_18"
android:layout_centerVertical="true"/>
<TextView
android:layout_toRightOf="@id/left_image"
android:id="@+id/setting_left_text"
android:layout_centerVertical="true"
android:textSize="@dimen/sp_15"
android:textColor="@color/c_4d4d4d"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/right_text"
android:layout_toLeftOf="@id/setting_right_icon"
android:layout_width="wrap_content"
android:textSize="@dimen/sp_14"
android:textColor="@color/c_999999"
android:layout_centerVertical="true"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"/>
<ImageView
android:layout_marginLeft="@dimen/dp_8"
android:id="@+id/setting_right_icon"
android:layout_centerVertical="true"
android:src="@mipmap/ic_right_arrow_gray"
android:layout_width="@dimen/dp_14"
android:layout_alignParentRight="true"
android:layout_height="@dimen/dp_14" />
</RelativeLayout>
在res\values下新建一个 attrs.xml 和 dimens.xml
attrs.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SettingView">
<attr name="left_text_size" format="integer"></attr>
<attr name="left_text_color" format="color"></attr>
<attr name="left_text_content" format="string|reference"></attr>
<attr name="right_text_size" format="integer"></attr>
<attr name="right_text_color" format="color"></attr>
<attr name="right_text_content" format="string|reference"></attr>
<attr name="right_image_src" format="reference"></attr>
<attr name="right_image_visible" format="integer"></attr>
<attr name="left_image_src" format="reference"></attr>
<attr name="left_image_visible" format="reference"></attr>
</declare-styleable>
</resources>
colors.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
<color name="c_ffffff">#ffffff</color>
<color name="c_4d4d4d">#4d4d4d</color>
<color name="c_999999">#999999</color>
<color name="c_f6f6f6">#f6f6f6</color>
</resources>
dimens.xml 代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--边距-->
<dimen name="dp_0_5">0.5dp</dimen>
<dimen name="dp_8">8dp</dimen>
<dimen name="dp_14">14dp</dimen>
<dimen name="dp_15">15dp</dimen>
<dimen name="dp_16">16dp</dimen>
<dimen name="dp_18">18dp</dimen>
<dimen name="dp_48">48dp</dimen>
<!--设置字体大小-->
<dimen name="sp_14">14sp</dimen>
<dimen name="sp_15">15sp</dimen>
</resources>
strings.xml 代码
<resources>
<string name="app_name">SettingViewDemo</string>
<string name="name">姓名</string>
<string name="letter">英文字母</string>
</resources>
styles.xml 代码
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<!-- 统一分割线 -->
<style name="style_line">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">@dimen/dp_0_5</item>
<item name="android:background">@color/c_f6f6f6</item>
</style>
</resources>