6

(抱歉我的问题很糟糕。我现在已经更新了)

我怎样才能使它在 XML 文件中?我尝试使用以下代码,但不正确(我使用“android:rotation="-90" 进行旋转。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<FrameLayout
    android:layout_width="141dp"
    android:layout_height="200dp"
    android:layout_weight="0.41"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/sidebar_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/shape_card_sidebar"
        android:inputType="text"
        android:rotation="-90"
        android:text="I want to be like this" >
    </EditText>
</FrameLayout>

在此处输入图像描述

4

2 回答 2

2

You're going to run into several problems if you try to do it that way. The most obvious issue will be the incorrect measurement. Instead you should create a custom view. Something like this:

public class RotatedTextVew extends TextView {
    public RotatedTextView(Context context) {
        super(context);
    }

    public RotatedTextView(Context context, AttributeSet attrs) {
        super(context, attrs)
    }

    public RotatedTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // Switch dimensions
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.rotate(90);
        super.onDraw(canvas);
        canvas.restore();
    }
}

I haven't actually tested this, but this is how I'd start.

于 2013-04-22T18:49:02.857 回答
0

将您的FrameLayout替换为 < Relative > 或 < LinearLayout >。还要设置父布局的 android:gravity="center" 属性。

于 2013-04-23T06:54:22.043 回答