我創建了4個textviews編程和我加在我的佈局不工作,我有一個問題,重心財產不working.i的意思,當我提高我的TextView的文字大小重心Repealed.default文字大小14dp 請檢查我的代碼片段。機器人重心在TextView中
private class DigitView extends TextView {
public DigitView(Context context) {
this(context, null);
}
public DigitView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public DigitView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// If selected draw the accent
if (isSelected()) {
this.setBackgroundResource(R.drawable.pincode_background_border_select);
}
else
{
this.setBackgroundResource(R.drawable.pincode_background_border_unselect);
}
}
}
for (int i = 0; i < mDigits; i++) {
DigitView digitView = new DigitView(getContext());
digitView.setWidth(valueInPixels);
digitView.setHeight(valueInPixels);
digitView.setTextColor(Color.WHITE);
digitView.setTextSize(mDigitTextSize);
digitView.setGravity(Gravity.CENTER);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
digitView.setElevation(mDigitElevation);
}
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i > 0) {
lp.leftMargin = 10; // margin goes here. Use marginStart instead to support right-to-left layouts
}
childView.addView(digitView, lp);
}
,這是我的佈局XML代碼
<LinearLayout
android:id="@+id/my_Container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<LinearLayout
android:id="@+id/my_Container_child"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</LinearLayout>
</LinearLayout>
my_Container_child is my layout
我到底做錯了什麼?如果有人知道的解決方案,請幫助我 感謝
將佈局引力屬性添加到LinearLayout –
您想要textView中的textView或linearLayout中的textView以居中爲中心嗎?還有哪個佈局是childView? –
@yogi我更新了我的問題。請參閱我的結果圖片 – donoachua