android - 容器,如 div、span 等

标签 android android-layout

是否可以创建像 div 这样的容器? 问题是我有 4 个控件(TextViewButtonsEditView),我想将它们居中。但是我没有一个一个地做,而是想知道是否有一种聪明的方法可以做到这一点。在 Web 应用程序中,使用 css,我们可以在 div 上执行此操作:

margin-left: auto;
margin-right: auto;
height: xxx;
width: yyy;

然后 div 将在页面上居中

在创建 Android 应用程序时是否有类似的方法?

最佳答案

在 XML 中,Div 相当于 ViewGroup

例如:LinearLayoutRelativeLayout

ViewGroups 可以包含 View 和控件,例如:TextViewButtonEditView

More ViewGroup Subclasses

我创建了一个我认为您要问的示例:

我使用 LinearLayout 来保存我们的 View 和控件。

然后我在我们的 LinearLayout 上使用 android:gravity="center_horizo​​ntal" 来让 child 居中。

<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:orientation="vertical"
    android:gravity="center_horizontal"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="200dp"
        android:gravity="center_horizontal"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <Button
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    <EditView
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

这是它的样子:

Demo

关于android - 容器,如 div、span 等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762096/

相关文章:

java - jsoup 在异步任务 Android 中解析 HTML

android - 如何在 Android 客户端上访问特定的 Google Cloud Endpoints API 版本

java - 在 Android 中运行方法时显示加载对话框?

java - Android 通知播放一次声音

java - 在 ExampleInstrumentedTest.java 文件中获取错误

java - Android 小部件点击时波纹背景

android - 如何显示半透明 Activity ?

android - 我的相机 View 与 GLSurfaceView 重叠...但我希望它被 GLSurfaceView 覆盖

java - android从布局xml动态添加元素

android - 线性布局 - weight 和 FILL_PARENT 的区别