我是Android开发的新手。我想知道用于设计XML布局的不同方法有哪些。我一直在使用eclipse的拖放界面,我在做一些搜索时看到了http://droiddraw.org/。只是想知道是否有其他可能更好的方法来设计专业人员使用的布局,因为我很难使用eclipse界面进行复杂的设计?
发布于 2011-12-02 09:01:46
首先,查看android开发人员站点用户界面页面
http://developer.android.com/guide/topics/ui/index.html
基本上,有三种不同的方法可以创建android布局:
XML
http://developer.android.com/guide/topics/ui/declaring-layout.html
您可以使用XML定义静态布局。也许考虑它的一个好方法是一种速记。声明视图和属性非常容易,而且XML的分层格式使其更易于可视化。
这是一个典型的布局
<?xml version="1.0" encoding="utf-8"?>
<ViewGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:attribute="value" >
<ViewGroup android:attribute="value" >
<View android:attribute="value" />
</ViewGroup>
<View android:attribute="value" />
</ViewGroup>然后,在您的活动中使用setContentView(R.layout.layout),开始您的业务。
Java
您可以在XML中做所有需要做的事情,再加上添加侦听器之类的内容,或者做其他在XML中不能做的动态事情。下面是你如何声明一个典型的布局(ViewGroup是抽象的,所以你必须使用子类。XML也是如此)
ViewGroup parent = new ViewGroup(this);
ViewGroup vg1 = new ViewGroup(this);
View v1 = new View(this);
View v2 = new View(this);
parent.addView(vg1);
vg1.addView(v1);
parent.addView(v2);
v1.setOnAwesomeListener(new AwesomeListener() {
onAwesome(View v) {
doDynamicThings();
}
}
setContentView(parent);混合
在我看来,这是最常用的情况。在XML中声明一个带有id的布局,比如android:id="@+id/v1",然后将视图从XML加载到Java中。
setContentView(R.layout.layout);
View v1 = findViewById(R.id.v1);
// dynamically change v1发布于 2011-12-02 10:37:30
如何使用XML设计布局
因此,由于缺乏GUI设计器工具,您别无选择,只能手动编写布局代码。好消息是,一旦你掌握了它的诀窍,你应该能够处理任何你想要的布局。让我们看一下构建块
首先,您需要选择一个ViewGroup来定义布局的结构或布局的部分。请记住,这些部分可以嵌套,因此可以自上而下地设计,并尝试根据您希望它们具有的形式对布局的各个部分进行分类。主要有两个选项:
顾名思义,这对于在一行中排列项目很有用。选择一个orientation,水平或垂直,然后简单地添加项目。它们将按从上到下或从左到右的顺序添加。
用于将项目放置在屏幕上的特定位置。因此,如果你想在左上角放置一个按钮,或者在顶部放置一个条,这是你的ViewGroup。
用于定义视图的宽度、高度、重量和其他方面。
宽度和高度有两个选项:fill_parent (在API level 8中被替换为match_parent )和wrap_content。视图可以选择填充父视图的宽度,也可以只占用所需的空间。
还有另一个有用的布局参数,称为weight,这是LinearLayout独有的。它对于让视图按比例共享空间,或者让一个视图占用在LinearLayout中的其他视图占用其份额后留下的空间非常有用。
示例
让我们试着设计一下Google Maps的布局。假设这是我脑海中的布局,我想要实现它。这是一个截图

我将试着将其分解:
看着它,上面有一个横跨它的条形图,下面有一张地图。我相信这可以用LinearLayout或RelativeLayout来实现。但是,右下方和左下方的按钮显示为RelativeLayout,因此我将使用该按钮。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TODO:BAR
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
</TODO:BAR>
<MapView
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/bar" />
<ImageButton
android:id="@+id/latitude"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="20dp"
android:layout_marginLeft="20dp" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical" >
<ImageButton
android:id="@+id/zoom_in"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/zoom_out"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>现在给出一些解释。在RelativeLayout中,您可以使用alignParent[direction]来指定视图的位置。我还想在边上留出一些空间,所以我使用margin[direction]来指定dp或与密度无关的像素。正如您所看到的,wrap_content大部分时间都在使用,因此这些按钮将获取它们所使用的图像的大小。
现在,除了顶部的栏之外,所有内容都已定义。我将把它分成四个不同的视图:下拉菜单视图、搜索视图、图层按钮和我的位置按钮。我希望它的工作方式是将菜单放在最左边,图层和我的位置按钮放在右边,搜索框占据剩余的空间。这听起来像是LinearLayout和weight的工作!下面是我定义栏的方式,可以将它插入到上面的占位符中,以获得最终布局
<LinearLayout
android:id="@+id/bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<ImageButton
android:id="@+id/dropdown_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/search"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageButton
android:id="@+id/layers"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/my_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>将搜索栏的宽度设置为0dp意味着让其他视图获取它们所需的内容,然后权重表示获取剩余空间。
现在你就知道了。重现了Google Maps应用程序的基本布局(去掉按钮图像和其他细节,如自定义视图),展示了如何轻松地使用各种布局和XML。希望这是有用的。
发布于 2011-12-02 09:22:53
工具链在这方面有点薄弱。我真的不太喜欢DroidDraw,Eclipse GUI编辑器除了简单的布局之外,并不是很好。例如,它经常不正确地呈现RelativeLayouts。
就我个人而言,我几乎所有的工作都是直接用XML完成的。你必须了解所有不同的布局类是如何工作的,才能做任何复杂的事情。XML唯一真正的缺点是,所有来自标记、属性等的额外繁琐使得需要输入大量额外的内容,但编辑器会为您处理大部分内容。
https://stackoverflow.com/questions/8350409
复制相似问题