Skip to content

Commit 683cfea

Browse files
committed
修复标题跳动的问题,优化部分属性和代码
1 parent e15093e commit 683cfea

File tree

13 files changed

+106
-84
lines changed

13 files changed

+106
-84
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@
1313
#### 集成步骤
1414

1515
dependencies {
16-
implementation 'com.hjq:titlebar:3.3'
16+
implementation 'com.hjq:titlebar:3.5'
1717
}
1818

1919
#### 属性大全(划重点,要考)
2020

21-
<declare-styleable name="TitleBar" >
21+
<declare-styleable name="TitleBar">
2222
<!-- 标题栏的样式 -->
2323
<attr name="bar_style">
2424
<enum name="light" value="0x10" />
@@ -46,8 +46,9 @@
4646
<attr name="background_left" format="reference|color" />
4747
<attr name="background_right" format="reference|color" />
4848
<!-- 分割线 -->
49-
<attr name="line" format="boolean" />
50-
<attr name="color_line" format="color" />
49+
<attr name="line_visible" format="boolean" />
50+
<attr name="line_color" format="color" />
51+
<attr name="line_size" format="dimension" />
5152
</declare-styleable>
5253

5354
#### XML示例
@@ -62,20 +63,20 @@
6263
#### 设置监听事件
6364

6465
mTitleBar.setOnTitleBarListener(new OnTitleBarListener() {
65-
66+
6667
@Override
6768
public void onLeftClick(View v) {
68-
Toast.makeText(MainActivity.this, "左项被点击", Toast.LENGTH_SHORT).show();
69+
ToastUtils.show("左项View被点击");
6970
}
70-
71+
7172
@Override
7273
public void onTitleClick(View v) {
73-
Toast.makeText(MainActivity.this, "标题被点击", Toast.LENGTH_SHORT).show();
74+
ToastUtils.show("中间View被点击");
7475
}
75-
76+
7677
@Override
7778
public void onRightClick(View v) {
78-
Toast.makeText(MainActivity.this, "右项被点击", Toast.LENGTH_SHORT).show();
79+
ToastUtils.show("右项View被点击");
7980
}
8081
});
8182

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.bar.demo"
88
minSdkVersion 14
99
targetSdkVersion 26
10-
versionCode 33
11-
versionName "3.3"
10+
versionCode 35
11+
versionName "3.5"
1212
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
1313
}
1414
buildTypes {

app/src/main/java/com/hjq/bar/demo/MainActivity.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.os.Bundle;
44
import android.support.v7.app.AppCompatActivity;
55
import android.view.View;
6-
import android.widget.Toast;
76

87
import com.hjq.bar.OnTitleBarListener;
98
import com.hjq.bar.TitleBar;

app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
android:layout_width="match_parent"
9494
android:layout_height="wrap_content"
9595
android:layout_marginTop="20dp"
96-
app:line="false"
96+
app:line_visible="false"
9797
app:title="不带分割线的标题栏" />
9898

9999
<com.hjq.bar.TitleBar

library/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ android {
99

1010
defaultConfig {
1111
targetSdkVersion 26
12-
versionCode 33
13-
versionName "3.3"
12+
versionCode 35
13+
versionName "3.5"
1414
}
1515
}
1616

1717
publish {
1818
userOrg = 'getactivity'//填写bintray用户名,注意大小写
1919
groupId = 'com.hjq'//定义的maven group id最终引用形式
2020
artifactId = 'titlebar'//maven的artifact id
21-
version = '3.3'//maven 上发布版本号
21+
version = '3.5'//maven 上发布版本号
2222
description = 'A simple generic title bar'//描述,自己定义
2323
website = "https://github.com/getActivity/TitleBar"//项目在github中的地址
2424
}

library/src/main/java/com/hjq/bar/ITitleBarStyle.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public interface ITitleBarStyle {
2020
float getTitleViewSize(); // 标题文本大小
2121
float getRightViewSize(); // 右边文本大小
2222

23-
boolean getLineVisibility(); // 分割线是否可见
24-
int getLineBackgroundColor(); // 分割线背景颜色
23+
boolean isLineVisible(); // 分割线是否可见
24+
int getLineColor(); // 分割线背景颜色
25+
int getLineSize(); // 分割线的大小
2526

2627
int getLeftViewBackground(); // 左边背景资源
2728
int getRightViewBackground(); // 右边背景资源

library/src/main/java/com/hjq/bar/TitleBar.java

Lines changed: 64 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.util.AttributeSet;
1010
import android.util.TypedValue;
1111
import android.view.View;
12+
import android.view.ViewGroup;
1213
import android.widget.FrameLayout;
1314
import android.widget.LinearLayout;
1415
import android.widget.TextView;
@@ -70,12 +71,12 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
7071
heightMeasureSpec = MeasureSpec.makeMeasureSpec(titleBarHeight, MeasureSpec.EXACTLY);
7172
}
7273

73-
Drawable background = getBackground();
74+
final Drawable background = getBackground();
7475
// 如果当前背景是一张图片的话
7576
if (background instanceof BitmapDrawable) {
7677
mMainLayout.getLayoutParams().height = MeasureSpec.getSize(heightMeasureSpec);
7778
// 算出标题栏的宽度和图片的宽度之比例
78-
double ratio = (double) MeasureSpec.getSize(widthMeasureSpec) / (double) background.getIntrinsicWidth();
79+
final double ratio = (double) MeasureSpec.getSize(widthMeasureSpec) / (double) background.getIntrinsicWidth();
7980
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (ratio * background.getIntrinsicHeight()), MeasureSpec.EXACTLY);
8081
}
8182
}
@@ -141,18 +142,17 @@ private void initStyle(AttributeSet attrs) {
141142
}
142143
}
143144

145+
// 图标设置
144146
if (ta.hasValue(R.styleable.TitleBar_title_right)) {
145147
setRightTitle(ta.getString(R.styleable.TitleBar_title_right));
146148
}
147149

148150
if (ta.hasValue(R.styleable.TitleBar_icon_left)) {
149151
setLeftIcon(getContext().getResources().getDrawable(ta.getResourceId(R.styleable.TitleBar_icon_left, 0)));
150152
}else {
151-
if (ta.getBoolean(R.styleable.TitleBar_icon_back, true)) {
153+
if (ta.getBoolean(R.styleable.TitleBar_icon_back, style.getBackIconResource() > 0)) {
152154
// 显示返回图标
153-
if (style.getBackIconResource() != 0) {
154-
setLeftIcon(getContext().getResources().getDrawable(style.getBackIconResource()));
155-
}
155+
setLeftIcon(getContext().getResources().getDrawable(style.getBackIconResource()));
156156
}
157157
}
158158

@@ -171,16 +171,13 @@ private void initStyle(AttributeSet attrs) {
171171
setRightSize(TypedValue.COMPLEX_UNIT_PX, ta.getDimensionPixelSize(R.styleable.TitleBar_size_right, ViewBuilder.sp2px(getContext(), style.getRightViewSize())));
172172

173173
// 背景设置
174-
if (style.getLeftViewBackground() != 0) {
175-
setLeftBackground(ta.getResourceId(R.styleable.TitleBar_background_left, style.getLeftViewBackground()));
176-
}
177-
if (style.getRightViewBackground() != 0) {
178-
setRightBackground(ta.getResourceId(R.styleable.TitleBar_background_right, style.getRightViewBackground()));
179-
}
174+
setLeftBackground(ta.getResourceId(R.styleable.TitleBar_background_left, style.getLeftViewBackground()));
175+
setRightBackground(ta.getResourceId(R.styleable.TitleBar_background_right, style.getRightViewBackground()));
180176

181177
// 分割线设置
182-
setLineVisible(ta.getBoolean(R.styleable.TitleBar_line, style.getLineVisibility()));
183-
setLineColor(ta.getColor(R.styleable.TitleBar_color_line, style.getLineBackgroundColor()));
178+
setLineVisible(ta.getBoolean(R.styleable.TitleBar_line_visible, style.isLineVisible()));
179+
setLineColor(ta.getColor(R.styleable.TitleBar_line_color, style.getLineColor()));
180+
setLineSize(ta.getDimensionPixelSize(R.styleable.TitleBar_line_size, style.getLineSize()));
184181

185182
// 回收TypedArray
186183
ta.recycle();
@@ -219,6 +216,23 @@ public void run() {
219216
}
220217
}
221218

219+
/**
220+
* {@link View.OnClickListener}
221+
*/
222+
@Override
223+
public void onClick(View v) {
224+
if (getOnTitleBarListener() == null) return;
225+
226+
final int id = v.getId();
227+
if (id == R.id.bar_id_left_view) {
228+
getOnTitleBarListener().onLeftClick(v);
229+
}else if (id == R.id.bar_id_title_view) {
230+
getOnTitleBarListener().onTitleClick(v);
231+
}else if (id == R.id.bar_id_right_view) {
232+
getOnTitleBarListener().onRightClick(v);
233+
}
234+
}
235+
222236
@Override
223237
protected void onAttachedToWindow() {
224238
super.onAttachedToWindow();
@@ -237,23 +251,6 @@ protected void onDetachedFromWindow() {
237251
super.onDetachedFromWindow();
238252
}
239253

240-
/**
241-
* {@link View.OnClickListener}
242-
*/
243-
@Override
244-
public void onClick(View v) {
245-
if (getOnTitleBarListener() == null) return;
246-
247-
final int id = v.getId();
248-
if (id == R.id.bar_id_left_view) {
249-
getOnTitleBarListener().onLeftClick(v);
250-
}else if (id == R.id.bar_id_title_view) {
251-
getOnTitleBarListener().onTitleClick(v);
252-
}else if (id == R.id.bar_id_right_view) {
253-
getOnTitleBarListener().onRightClick(v);
254-
}
255-
}
256-
257254
/**
258255
* 获取点击监听器
259256
*/
@@ -278,61 +275,65 @@ public CharSequence getTitle() {
278275
/**
279276
* 设置标题
280277
*/
281-
public void setTitle(int resourceId) {
282-
setTitle(getResources().getString(resourceId));
278+
public void setTitle(int stringId) {
279+
setTitle(getResources().getString(stringId));
283280
}
284281

285282
public void setTitle(CharSequence text) {
286283
mTitleView.setText(text);
287-
postDelayed(this, 30);
284+
post(this);
288285
}
289286

290287
/**
291288
* 设置左边的标题
292289
*/
293-
public void setLeftTitle(int resourceId) {
294-
setLeftTitle(getResources().getString(resourceId));
290+
public void setLeftTitle(int stringId) {
291+
setLeftTitle(getResources().getString(stringId));
295292
}
296293

297294
public void setLeftTitle(CharSequence text) {
298295
mLeftView.setText(text);
299-
postDelayed(this, 30);
296+
post(this);
300297
}
301298

302299
/**
303300
* 设置右边的标题
304301
*/
305-
public void setRightTitle(int resourceId) {
306-
setRightTitle(getResources().getString(resourceId));
302+
public void setRightTitle(int stringId) {
303+
setRightTitle(getResources().getString(stringId));
307304
}
308305

309306
public void setRightTitle(CharSequence text) {
310307
mRightView.setText(text);
311-
postDelayed(this, 30);
308+
post(this);
312309
}
313310

314311
/**
315312
* 设置左边的图标
316313
*/
317-
public void setLeftIcon(int resourceId) {
318-
setLeftIcon(getContext().getResources().getDrawable(resourceId));
314+
public void setLeftIcon(int iconId) {
315+
if (iconId > 0) {
316+
setLeftIcon(getContext().getResources().getDrawable(iconId));
317+
}
319318
}
320319

321320
public void setLeftIcon(Drawable drawable) {
322321
mLeftView.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
323-
postDelayed(this, 30);
322+
post(this);
324323
}
325324

326325
/**
327326
* 设置右边的图标
328327
*/
329-
public void setRightIcon(int resourceId) {
330-
setRightIcon(getContext().getResources().getDrawable(resourceId));
328+
public void setRightIcon(int iconId) {
329+
if (iconId > 0) {
330+
setRightIcon(getContext().getResources().getDrawable(iconId));
331+
}
331332
}
332333

333334
public void setRightIcon(Drawable drawable) {
334335
mRightView.setCompoundDrawablesWithIntrinsicBounds(null, null, drawable, null);
335-
postDelayed(this, 30);
336+
post(this);
336337
}
337338

338339
/**
@@ -359,8 +360,10 @@ public void setRightColor(int color) {
359360
/**
360361
* 设置左标题状态选择器
361362
*/
362-
public void setLeftBackground(int resourceId) {
363-
mLeftView.setBackgroundResource(resourceId);
363+
public void setLeftBackground(int bgId) {
364+
if (bgId > 0) {
365+
mLeftView.setBackgroundResource(bgId);
366+
}
364367
}
365368

366369
public void setLeftBackground(Drawable drawable) {
@@ -374,8 +377,10 @@ public void setLeftBackground(Drawable drawable) {
374377
/**
375378
* 设置右标题状态选择器
376379
*/
377-
public void setRightBackground(int resourceId) {
378-
mRightView.setBackgroundResource(resourceId);
380+
public void setRightBackground(int bgId) {
381+
if (bgId > 0) {
382+
mRightView.setBackgroundResource(bgId);
383+
}
379384
}
380385

381386
public void setRightBackground(Drawable drawable) {
@@ -421,6 +426,15 @@ public void setLineColor(int color) {
421426
mLineView.setBackgroundColor(color);
422427
}
423428

429+
/**
430+
* 设置分割线的大小
431+
*/
432+
public void setLineSize(int size) {
433+
ViewGroup.LayoutParams layoutParams = mLineView.getLayoutParams();
434+
layoutParams.height = size;
435+
mLineView.setLayoutParams(layoutParams);
436+
}
437+
424438
/**
425439
* 获取主要的布局对象
426440
*/

library/src/main/java/com/hjq/bar/ViewBuilder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
*/
2323
final class ViewBuilder {
2424

25-
private LinearLayout mMainLayout;
26-
private TextView mLeftView;
27-
private TextView mTitleView;
28-
private TextView mRightView;
25+
private final LinearLayout mMainLayout;
2926

30-
private View mLineView;
27+
private final TextView mLeftView;
28+
private final TextView mTitleView;
29+
private final TextView mRightView;
30+
31+
private final View mLineView;
3132

3233
ViewBuilder(Context context) {
3334
mMainLayout = new LinearLayout(context);

library/src/main/java/com/hjq/bar/style/TitleBarLightStyle.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,20 @@ public float getRightViewSize() {
5757
}
5858

5959
@Override
60-
public boolean getLineVisibility() {
60+
public boolean isLineVisible() {
6161
return true;
6262
}
6363

6464
@Override
65-
public int getLineBackgroundColor() {
65+
public int getLineColor() {
6666
return 0xFFECECEC;
6767
}
6868

69+
@Override
70+
public int getLineSize() {
71+
return 1;
72+
}
73+
6974
@Override
7075
public int getLeftViewBackground() {
7176
return R.drawable.bar_selector_selectable_white;

0 commit comments

Comments
 (0)