将图像视图移动到左侧会使其变小

时间:2013-09-26 06:42:43

标签: android android-layout android-emulator

我无法向左移动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个屏幕显示:

Not Working Working

0 个答案:

没有答案