如何在android studio中垂直对齐不同的元素?

时间:2014-12-31 08:48:15

标签: android android-layout android-studio

我是android开发的新手。我试图垂直对齐我的页面元素,但无法做到这一点。我不知道,我如何在Java中使用前端布局控件。

我的main.xml文件如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="30dip"/>-->
    <TextView
        android:text="@string/getVerse"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:textSize="@dimen/header"
        android:layout_gravity="center_horizontal"
     />

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="20dip"-->
     <!--/>-->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/questionSize"
        android:hint="@string/questionHint"
        />
    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="25dip"-->
    <!--/>-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/buttonTextSize"
        android:text="@string/buttonText">
    </Button>

</LinearLayout>

页面如下:

screenshot of avd 请帮帮我!!!

2 个答案:

答案 0 :(得分:5)

只需在android:orientation="vertical"中使用LinearLayout属性即可。您将获得对齐Horizontally的元素,因为这是android提供的默认值。

执行此操作后的代码如下所示:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="30dip"/>-->
    <TextView
        android:text="@string/getVerse"
        android:layout_width="wrap_content"
        android:layout_height="100dip"
        android:textSize="@dimen/header"
        android:layout_gravity="center_horizontal"
     />

    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="20dip"-->
     <!--/>-->
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/questionSize"
        android:hint="@string/questionHint"
        />
    <!--<View-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="25dip"-->
    <!--/>-->

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="@dimen/buttonTextSize"
        android:text="@string/buttonText">
    </Button>

    </LinearLayout>

希望这有帮助!

答案 1 :(得分:3)

将以下代码添加到您的父级LinearLayout

 android:orientation="vertical"

在活动中显示Gif:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new GifView(this));
    }

    static class GifView extends View {
        Movie movie;
        GifView(Context context) {
            super(context);
            File myFile = new File("/sdcard/test.gif");
            InputStream fis = null;
            try {
                fis = new BufferedInputStream(new FileInputStream(myFile),
                        16 * 1024);
                fis.mark(16 * 1024);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            movie = Movie.decodeStream(fis);
            Log.e("SS", "movie.duration()" + movie.duration());

        }

        @Override
        protected void onDraw(Canvas canvas) {
            try {
                if (movie != null) {
                    movie.setTime((int) SystemClock.uptimeMillis()
                            % movie.duration());
                    movie.draw(canvas, 0, 0);
                    invalidate();
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }

        }
    }
相关问题