Skip to content

Commit

Permalink
新增分行显示上下两行对齐方式的属性
Browse files Browse the repository at this point in the history
  • Loading branch information
lygttpod committed Nov 24, 2016
1 parent 23b864b commit 39a5b79
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 126 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.allen.library.CommonTextView;

/**
* Created by allen on 2016/11/22.
*/

public class CommonTextViewActivity extends AppCompatActivity {
private CommonTextView commonTextView;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.common_textview_layout);
commonTextView.getLeftTextString();
commonTextView.getCenterTextString();
commonTextView.getRightTextString();
}
}
228 changes: 110 additions & 118 deletions app/src/main/res/layout/common_textview_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,137 +5,129 @@
android:layout_height="match_parent"
android:orientation="vertical">

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftTextColor="@color/colorAccent"
ctv:cLeftTextSize="15dp"
ctv:cLeftTextString="帐号管理"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"
ctv:cShowDividerLineType="none" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftTextString="清除缓存"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"
ctv:cRightTextColor="@color/colorAccent"
ctv:cRightTextString="20.5M"
ctv:cShowDividerLineType="both"
ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconResForDrawableLeft="@drawable/uu"
ctv:cLeftTextColor="@color/colorAccent"
ctv:cLeftTextSize="15dp"
ctv:cLeftTextString="收获地址" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterTextString="中间文字"
ctv:cLeftTextString="左边文字"
ctv:cRightTextString="右边文字" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconResForDrawableLeft="@drawable/scatter_time"
ctv:cLeftTextString="起息时间"
ctv:cRightTextString="T+1日" />

<com.allen.library.CommonTextView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconResForDrawableLeft="@drawable/scatter_time"
ctv:cLeftTextString="起息时间"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"
ctv:cRightTextString="T+1日" />
android:layout_height="match_parent">
<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftTextString="清除缓存"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"
ctv:cRightTextColor="@color/colorAccent"
ctv:cRightTextString="20.5M"
ctv:cShowDividerLineType="both"
ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterIconResForDrawableTop="@drawable/scatter_use"
ctv:cCenterTextString="还款方式"
ctv:cLeftIconResForDrawableTop="@drawable/scatter_time"
ctv:cLeftTextString="起息时间"
ctv:cRightIconResForDrawableTop="@drawable/scatter_type"
ctv:cRightTextString="截至日期" />
<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconResForDrawableLeft="@drawable/uu"
ctv:cLeftTextColor="@color/colorAccent"
ctv:cLeftTextSize="15dp"
ctv:cLeftTextString="收获地址" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cCenterTextString="中间文字"
ctv:cLeftTextString="左边文字"
ctv:cRightTextString="右边文字" />
<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="60dp"
ctv:cShowDividerLineType="bottom"
ctv:cCenterTextString="年龄\n250"
ctv:cLeftTextViewLineSpacingExtra="8dp"
ctv:cCenterTextViewLineSpacingExtra="8dp"
ctv:cRightTextViewLineSpacingExtra="8dp"
ctv:cSetSingleLine="false"
ctv:cLeftTextString="姓名\n欧阳峰鸟"
ctv:cRightTextString="学籍\n北京科技大学" />

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextColor="@color/colorAccent"
ctv:cCenterTextSize="15dp"
ctv:cCenterTextString="帐号管理"
android:layout_width="match_parent"
android:layout_height="60dp"
ctv:cShowDividerLineType="bottom"
ctv:cLeftIconResForDrawableLeft="@drawable/head_default"
ctv:cLeftTextViewLineSpacingExtra="8dp"
ctv:cCenterTextViewLineSpacingExtra="8dp"
ctv:cRightTextViewLineSpacingExtra="8dp"
ctv:cSetSingleLine="false"
ctv:cLeftTextString="姓名\n欧阳峰"
ctv:cLeftTextViewGravity="left_center"
ctv:cRightTextString="已实名"
ctv:cRightTextColor="@color/colorAccent"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"/>

ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextColor="@color/colorAccent"
ctv:cCenterTextSize="15dp"
ctv:cCenterTextString="帐号管理"
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconResForDrawableLeft="@drawable/scatter_time"
ctv:cLeftTextString="起息时间"
ctv:cRightIconResForDrawableRight="@drawable/arrow_right_red"
ctv:cRightTextString="T+1日" />

ctv:cUseRipple="true" />
<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="55dp"
ctv:cCenterIconResForDrawableTop="@drawable/scatter_use"
ctv:cCenterTextString="还款方式"
ctv:cLeftIconResForDrawableTop="@drawable/scatter_time"
ctv:cLeftTextString="起息时间"
ctv:cRightIconResForDrawableTop="@drawable/scatter_type"
ctv:cRightTextString="截至日期" />

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

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextString="文字"
ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextString="文字"
ctv:cUseRipple="true" />

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextString="文字"
ctv:cUseRipple="true" />
</LinearLayout>

<com.allen.library.CommonTextView
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="1"
ctv:cCenterIconResForDrawableTop="@mipmap/ic_launcher"
ctv:cCenterTextColor="@color/colorAccent"
ctv:cCenterTextSize="15dp"
ctv:cCenterTextString="帐号管理"
android:layout_width="match_parent"
android:layout_height="80dp"
ctv:cShowDividerLineType="both"
ctv:cLeftIconResForDrawableLeft="@mipmap/ic_launcher"
ctv:cRightIconResForDrawableRight="@mipmap/ic_launcher"
ctv:cCenterTextString="张三\n188******88"
ctv:cLeftTextString="张三\n188******88"
ctv:cLeftTextViewGravity="left_center"
ctv:cRightTextString="张三\n188******88"
ctv:cCenterTextViewGravity="center"
ctv:cRightTextViewGravity="right_center"
ctv:cSetSingleLine="false"
ctv:cUseRipple="true" />
</LinearLayout>
</ScrollView>

<com.allen.library.CommonTextView
android:layout_width="match_parent"
android:layout_height="50dp"
ctv:cLeftIconDrawablePadding="10dp"
ctv:cLeftIconResForDrawableLeft="@drawable/uu"
ctv:cLeftTextColor="@color/colorAccent"
ctv:cLeftTextSize="15dp"
ctv:cLeftTextString="左边文字"
ctv:cLeftViewPaddingLeft="10dp"
ctv:cLeftViewPaddingRight="10dp"

ctv:cCenterIconDrawablePadding="10dp"
ctv:cCenterIconResForDrawableLeft="@drawable/uu"
ctv:cCenterTextColor="@color/colorAccent"
ctv:cCenterTextSize="15dp"
ctv:cCenterTextString="中间文字"
ctv:cCenterViewPaddingLeft="10dp"
ctv:cCenterViewPaddingRight="10dp"

ctv:cRightIconDrawablePadding="10dp"
ctv:cRightIconResForDrawableLeft="@drawable/uu"
ctv:cRightTextColor="@color/colorAccent"
ctv:cRightTextSize="15dp"
ctv:cRightTextString="右边文字"
ctv:cRightViewPaddingLeft="10dp"
ctv:cRightViewPaddingRight="10dp"

ctv:cDividerLineColor="@color/colorPrimary"
ctv:cDividerLineHeight="1dp"
ctv:cBothDividerLineMarginLR="10dp"
ctv:cShowDividerLineType="both"
ctv:cBackgroundColor="@color/colorAccent"
ctv:cUseRipple="true"
ctv:cSetLines="1"
ctv:cSetMaxEms="10"
ctv:cSetSingleLine="true" />
</LinearLayout>
37 changes: 36 additions & 1 deletion library/src/main/java/com/allen/library/CommonTextView.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,19 @@ public class CommonTextView extends RelativeLayout {
private int mSetMaxEms = 10;
private int mSetLines = 1;

/**
* TextView的Gravity
*/
private static final int Gravity_Left_Center = 0;
private static final int Gravity_Center = 1;
private static final int Gravity_Right_Center = 2;

private static final int DEFAULT_Gravity = 1;

private int mLeftTextViewGravity;
private int mCenterTextViewGravity;
private int mRightTextViewGravity;

private TextView leftTextView, centerTextView, rightTextView;

private View topLineView, bottomLineView;
Expand Down Expand Up @@ -195,6 +208,10 @@ private void getAttr(AttributeSet attrs) {
mSetMaxEms = typedArray.getInt(R.styleable.CommonTextView_cSetMaxEms, mSetMaxEms);
mSetLines = typedArray.getInt(R.styleable.CommonTextView_cSetLines, 1);

mLeftTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cLeftTextViewGravity, DEFAULT_Gravity);
mCenterTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cCenterTextViewGravity, DEFAULT_Gravity);
mRightTextViewGravity = typedArray.getInt(R.styleable.CommonTextView_cRightTextViewGravity, DEFAULT_Gravity);

typedArray.recycle();
}

Expand Down Expand Up @@ -310,6 +327,7 @@ private void initLeftText() {
leftTextView.setText(mLeftTextString);
// leftTextView.setGravity(Gravity.CENTER_VERTICAL);
leftTextView.setLineSpacing(mLeftTextViewLineSpacingExtra, 1.0f);
setTextViewGravity(leftTextView, mLeftTextViewGravity);
}

setDrawable(leftTextView, mLeft_drawableLeft, mLeft_drawableTop, mLeft_drawableRight, mLeft_drawableBottom, mLeftIconDrawablePadding);
Expand All @@ -332,6 +350,7 @@ private void initCenterText() {
centerTextView = initText(centerTextView, centerTVParams, R.id.cCenterTextId, mCenterTextColor, mCenterTextSize);
centerTextView.setText(mCenterTextString);
centerTextView.setLineSpacing(mCenterTextViewLineSpacingExtra, 1.0f);
setTextViewGravity(centerTextView, mCenterTextViewGravity);

}
setDrawable(centerTextView, mCenter_drawableLeft, mCenter_drawableTop, mCenter_drawableRight, mCenter_drawableBottom, mCenterIconDrawablePadding);
Expand All @@ -354,6 +373,7 @@ private void initRightText() {
rightTextView.setText(mRightTextString);
// rightTextView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
rightTextView.setLineSpacing(mRightTextViewLineSpacingExtra, 1.0f);
setTextViewGravity(rightTextView, mRightTextViewGravity);

}
setDrawable(rightTextView, mRight_drawableLeft, mRight_drawableTop, mRight_drawableRight, mRight_drawableBottom, mRightIconDrawablePadding);
Expand Down Expand Up @@ -391,7 +411,7 @@ public TextView initText(TextView textView, LayoutParams layoutParams, int id, i
textView.setLayoutParams(layoutParams);
textView.setTextColor(textColor);
textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
textView.setGravity(Gravity.CENTER);
// textView.setGravity(Gravity.CENTER);
textView.setLines(mSetLines);
textView.setSingleLine(mSetSingleLine);
textView.setMaxEms(mSetMaxEms);
Expand All @@ -401,6 +421,21 @@ public TextView initText(TextView textView, LayoutParams layoutParams, int id, i
return textView;
}

private void setTextViewGravity(TextView textView, int gravity_type) {

switch (gravity_type) {
case Gravity_Left_Center:
textView.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
break;
case Gravity_Center:
textView.setGravity(Gravity.CENTER);
break;
case Gravity_Right_Center:
textView.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
}

}

/**
* 设置textView的drawable
*
Expand Down
17 changes: 17 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,22 @@
<attr name="cSetSingleLine" format="boolean" />
<attr name="cSetMaxEms" format="integer" />
<attr name="cSetLines" format="integer" />

<attr name="cLeftTextViewGravity" format="enum">
<enum name="left_center" value="0" />
<enum name="center" value="1" />
<enum name="right_center" value="2" />
</attr>
<attr name="cCenterTextViewGravity" format="enum">
<enum name="left_center" value="0" />
<enum name="center" value="1" />
<enum name="right_center" value="2" />
</attr>
<attr name="cRightTextViewGravity" format="enum">
<enum name="left_center" value="0" />
<enum name="center" value="1" />
<enum name="right_center" value="2" />
</attr>

</declare-styleable>
</resources>

0 comments on commit 39a5b79

Please sign in to comment.