不知道該如何控制TextView的 TextSize 和TextView大小 用inflate 填充xml佈局? 求大神指點下,為什麼我獲取我的控制元件的高設定給TextView ,TextView卻比期望的大出好多
使用
MyViewFlipper myViewFlipper = (MyViewFlipper) findViewById(R.id.mvf);
myViewFlipper.setStrings(titleList);
myViewFlipper.setOnItemClickListener(new MyViewFlipper.ItemClickListener() {
@Override
public void onItemClick(int position) {
Toast.makeText(ViewFlipperActivity.this, "position" + position, Toast.LENGTH_SHORT).show();
}
});
複製程式碼
/**
* desc:
* author: pdog
* email: pdog18@foxmail.com
* time: 2017/3/21 14 :33
*/
public class MyViewFlipper extends ViewFlipper implements View.OnClickListener {
private String DEFAULT_MESSAGE = "Default Message";
private int mCurrPos; //當前顯示的
private ArrayList<String> mStrings;
private Timer mTimer = new Timer();
private TextView mTextView;
TimerTask task = new TimerTask() {
@Override
public void run() {
post(new Runnable() {
@Override
public void run() {
moveNext();
}
});
}
};
TranslateAnimation in = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 1.0f,
Animation.RELATIVE_TO_SELF, 0.0f);
TranslateAnimation out = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f);
public MyViewFlipper(Context context) {
this(context, null);
}
public MyViewFlipper(Context context, AttributeSet attrs) {
super(context, attrs);
setOnClickListener(this);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
in.setDuration(500);
out.setDuration(500);
mTimer.schedule(task, 0, 4000);
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mTimer.cancel();
}
/**
* 需要顯示的陣列
*
* @param strings
*/
public void setStrings(@NonNull ArrayList<String> strings) {
if (strings == null) {
throw new NullPointerException("傳入的集合為null");
}
this.mStrings = strings;
}
private void moveNext() {
setView(mCurrPos, mCurrPos + 1);
setInAnimation(in);
setOutAnimation(out);
showNext();
}
private void setView(int curr, int next) {
if (mStrings == null || mStrings.size() == 0) {
if (mTextView == null) {
mTextView = new TextView(getContext());
addView(mTextView);
}
mTextView.setText(DEFAULT_MESSAGE);
return;
}
View view = LayoutInflater.from(getContext()).inflate(R.layout.viewflipper_item, this, false);
ImageView image = (ImageView) view.findViewById(R.id.img_viewflipper_item);
TextView tv = (TextView) view.findViewById(R.id.tv_viewflipper_item);
if ((curr < next) && (next > (mStrings.size() - 1))) {
next = 0;
} else if ((curr > next) && (next < 0)) {
next = mStrings.size() - 1;
}
tv.setText(mStrings.get(next));
if (getChildCount() > 1) {
removeViewAt(0);
}
addView(view, getChildCount());
mCurrPos = next;
}
ItemClickListener mItemClickListener;
public void setOnItemClickListener(ItemClickListener listener) {
this.mItemClickListener = listener;
}
interface ItemClickListener {
void onItemClick(int position);
}
@Override
public void onClick(View v) {
if (mItemClickListener != null) {
mItemClickListener.onItemClick(mCurrPos);
}
}
}
複製程式碼
<?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="40dp"
android:orientation="horizontal">\
<ImageView
android:id="@+id/img_viewflipper_item"
android:layout_width="100dp"
android:layout_height="40dp"
android:background="@mipmap/ic_launcher"/>
<TextView
android:id="@+id/tv_viewflipper_item"
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:text="@string/app_name"
android:textSize="30sp"/>
</LinearLayout>
複製程式碼