如何在Android Studio中使不同元素垂直对齐?

4

我是一个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>

页面如下图所示: avd的截屏 请帮我!!!

请看我的回答,@user3641971。 - gsanskar
@Lal 怎么可能是重复的?我在谷歌上搜索了这个问题,但没有找到任何结果。 - user3641971
请查看我评论中的链接。 - Lal
@Lal,我没有使用正确的搜索文本。 - user3641971
是的,当我搜索时,我得到了很多答案,这就是为什么我将其标记为重复的原因。 - Lal
2个回答

8

只需在您的LinearLayout中使用android:orientation="vertical"属性。由于这是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>

希望这能帮到您!

它有帮助。谢谢你。 - user3641971
你能分享一份相关的教程给我吗? - user3641971
@*_Sanskar 有两个答案,都是正确的。我应该接受哪一个呢? :) - user3641971

3
请将以下代码添加到您的父LinearLayout中。
 android:orientation="vertical"

在Activity中显示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();
            }

        }
    }

@PsyDuck,请告诉我您有什么查询? - Anjali
尚未完成。为了显示 gif,我会更新我的答案并提供代码。 - Anjali

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接