我无法将 ImageView 向左移动。我每次按一个按钮都必须这样做,通常我只能按8次。问题是,在第 4 步,图像会变小,无论工作平板电脑的分辨率如何。图像大小设置为 TextView 中的数字大小。在 10 英寸的平板电脑中,它有 51 个像素,而在 7 英寸的平板电脑中,它有 36 个像素。这个尺寸会一直保留,直到我第四次按下按钮,两个设备上的尺寸都变为 26。我真的被卡住了,不知道为什么会这样。也许你可以帮帮我。
这是xml:
<EditText android:id="@+id/secondNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#00000000" android:ellipsize="end" android:gravity="right" android:hint="@string/second_number_hint" android:inputType="number" android:maxLength="8" android:textSize="@dimen/number" /> <EditText android:id="@+id/firstNumber" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@id/secondNumber" android:layout_centerInParent="true" android:background="#00000000" android:ellipsize="end" android:focusable="true" android:focusableInTouchMode="true" android:gravity="right" android:hint="@string/first_number_hint" android:imeOptions="actionSend" android:inputType="number" android:maxLength="8" android:textAlignment="textEnd" android:textSize="@dimen/number" /> <View android:id="@+id/separator" android:layout_width="0px" android:layout_height="0.3dp" android:layout_alignLeft="@+id/secondNumber" android:layout_alignRight="@+id/secondNumber" android:layout_below="@+id/secondNumber" android:background="#000000" /> <Button android:id="@+id/nextStep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="@string/next" android:visibility="invisible" /> <Button android:id="@+id/previousStep" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/nextStep" android:text="@string/previous" android:visibility="invisible" /> <TextView android:id="@+id/additionSign" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/firstNumber" android:layout_alignBottom="@+id/firstNumber" android:layout_toRightOf="@+id/firstNumber" android:text="@string/addition_sign" android:textSize="@dimen/number" /> <Button android:id="@+id/newAddition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/previousStep" android:text="@string/new_addition" android:visibility="invisible" /> <ImageView android:id="@+id/secondAdditionArrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/firstAdditionArrow" android:layout_centerVertical="true" android:contentDescription="@string/content_description_show_first_number" android:src="@drawable/blue_arrow" android:visibility="invisible" /> <TextView android:id="@+id/additionMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/secondAdditionArrow" android:layout_toRightOf="@+id/firstAdditionArrow" android:text="@string/addition_message" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/blue_arrow_text_color" android:textSize="@dimen/arrow_message_size" android:textStyle="bold" android:visibility="invisible" /> <ImageView android:id="@+id/firstAdditionArrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/additionSign" android:layout_alignTop="@+id/additionSign" android:layout_toRightOf="@+id/additionSign" android:contentDescription="@string/content_description_show_first_number" android:src="@drawable/blue_arrow" android:visibility="invisible" /> <TextView android:id="@+id/finishedMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_below="@+id/result" android:layout_centerHorizontal="true" android:gravity="center|top" android:lines="5" android:maxEms="15" android:text="@string/addition_finish_message" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/red_arrow_color_text" android:textSize="@dimen/arrow_message_size" android:textStyle="bold" android:visibility="invisible" /> <TextView android:id="@+id/carryDigit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/firstNumber" android:layout_alignRight="@+id/firstNumber" android:text="@string/carry_digit" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/green_arrow_text_color" android:textSize="@dimen/number" android:textStyle="bold" android:visibility="invisible" /> <EditText android:id="@+id/result" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@id/separator" android:layout_below="@+id/separator" android:background="#00000000" android:ellipsize="end" android:enabled="false" android:gravity="right" android:hint="@string/result" android:inputType="number" android:maxLength="10" android:textSize="@dimen/number" > <requestFocus /> </EditText> <ImageView android:id="@+id/digitKeepArrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignRight="@+id/result" android:layout_below="@+id/result" android:contentDescription="@string/show_addition_result" android:src="@drawable/red_arrow" android:visibility="invisible" /> <TextView android:id="@+id/keedTheDigitMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/digitKeepArrow" android:layout_below="@+id/digitKeepArrow" android:gravity="fill|center" android:maxLines="1" android:minEms="10" android:text="@string/addition_message" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="@color/red_arrow_color_text" android:textSize="@dimen/arrow_message_size" android:textStyle="bold" android:visibility="invisible" /> <TextView android:id="@+id/carryDigitMessage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/carryDigitArrow" android:layout_alignLeft="@+id/carryDigitArrow" android:minEms="10" android:text="@string/carry_digit" android:textColor="@color/green_arrow_text_color" android:textSize="@dimen/arrow_message_size" android:textStyle="bold" android:visibility="invisible" /> <ImageView android:id="@+id/carryDigitArrow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/carryDigit" android:layout_alignRight="@+id/firstNumber" android:contentDescription="@string/show_carry_digit" android:src="@drawable/green_arrow" android:visibility="invisible" /> </RelativeLayout>
移动 ImageView 的函数是这样的:
Rect bounds = new Rect();
Paint textPaint = firstNumber.getPaint();
textPaint.getTextBounds(firstNumber.getText().toString(), 0,
firstNumber.getText().toString().length(), bounds);
int width = bounds.width();
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(
arrow.getWidth(), arrow.getHeight());
position.setMargins(
arrow.getLeft() - width / this.firstNumber.size(),
arrow.getTop(), arrow.getLeft(), arrow.getBottom());
arrow.setLayoutParams(position);
}
尺寸 xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="number">55dp</dimen>
<dimen name="arrow_message_size">32dp</dimen>
</resources>
不能直接发图,不好意思。
和 strings.xml 文件:
<resources>
<string name="app_name">Math Tutor</string>
<string name="title_main_activity">Math Tutor</string>
<string name="welcome_message">Welcome to Math Tutor! Choose an operation from the left side pane to get started.</string>
<string name="menu_title">Basic Math</string>
<string name="addition_sign">+</string>
<string name="solve">Solve</string>
<string name="next">Next</string>
<string name="previous">Previous</string>
<string name="insert_first_number">Insert first number</string>
<string name="first_step">First step</string>
<string name="second_step">Second step</string>
<string name="third_step">Third step</string>
<string name="first_number_hint">Number 1</string>
<string name="second_number_hint">Number 2</string>
<string name="result">Result</string>
<string name="new_addition">New Addition</string>
<string name="subtraction_sign">-</string>
<string name="number">Number</string>
<string name="exponent">Exponent</string>
<string name="equal_sign">=</string>
<string name="power">Power</string>
<string name="base">Base</string>
<string name="content_description_show_first_number">Show first number</string>
<string name="addition_message">Addition</string>
<string name="show_addition_result">Show addition result</string>
<string name="addition_finish_message">The operation is finished. Press New Addition for a new addition!</string>
<string name="carry_digit">1</string>
<string name="show_carry_digit">Show carry digit</string>
</resources>
奇怪的是,我已经用这种方法成功地移动了我想要的箭头,但是这个似乎并不想做我想做的事。
更新
我已经设法在第 4 步时修复了调整大小,但现在当我到达第 8 步时,我总是会拉伸图像。这是更新的代码:
Rect bounds = new Rect();
Paint textPaint = result.getPaint();
textPaint.getTextBounds(result.getText().toString(), 0, result
.getText().toString().length(), bounds);
int width = bounds.width();
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(
this.carryDigitArrowOriginalPosition.width,
this.carryDigitArrowOriginalPosition.height);
position.setMargins(this.carryDigitArrowOriginalPosition.leftMargin
- width, this.carryDigitArrowOriginalPosition.topMargin,
result.getRight(),
this.carryDigitArrowOriginalPosition.bottomMargin);
其余的都是一样的。我不明白为什么它会拉伸图像,即使它的宽度很大。以下是发生的 2 个屏幕: