我怎么能为Android做这个布局?

时间:2014-01-16 20:19:57

标签: android android-linearlayout

[求助]我有一个非常具体的布局,我想做。感觉它应该很容易创建,但我似乎无法用手指环绕它。所以我想创建一个如下图所示的布局。我到底该怎么做?我只是得到文本和2个图像,然后让按钮可见。也在下面发布我的布局。

提前致谢// eXpliCo。

http://imgur.com/Qu5CRDC

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_weight="20"
    android:orientation="vertical" >

     <TextView
         android:id="@+id/question_text"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="@string/question_text"
         android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:id="@+id/image_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/left_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/picture_placeholder" />

        <ImageView
            android:id="@+id/right_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/picture_placeholder" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/navigation_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Button" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Button" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/vote_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|center" >

        <Button
            android:id="@+id/vote_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Button" />

        <Button
            android:id="@+id/vote_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Button" />
    </LinearLayout>
</LinearLayout>
编辑:我现在得到了一个解决方案。我不知道它是否是最好的,但它对我有用。但是我还不能回答我自己的代码,所以我只是在这里发布我的解决方案。

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

    <TextView
        android:id="@+id/textView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:text="Test123 312 123 312"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="100" >

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/ic_launcher" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Test" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Test" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test" />

        <Button
            android:id="@+id/button4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="test" />

    </LinearLayout>

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

在您的第二个LinearLayout中,将orientation设置为horizontal

答案 1 :(得分:0)

你去吧

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView 
        android:text="Some Text here"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/SomeText"
        android:gravity="center"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/image_layout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/SomeText"
        android:orientation="horizontal" >


        <ImageView
            android:id="@+id/left_layout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_weight="1" />

        <ImageView
            android:id="@+id/right_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:layout_weight="1" />



    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_layout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/image_layout"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/vote_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 1"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />

        <Button
            android:id="@+id/vote_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 2"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/button_layout2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button_layout1"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/vote_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 3"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />

        <Button
            android:id="@+id/vote_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button 4"
            android:layout_weight="1"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp" />

    </LinearLayout>
</RelativeLayout>

答案 2 :(得分:0)

听起来这适用于较小的图像但不是较大的图像。如果是这种情况,请在每个子layout_weight中使用LinearLayout,以便每个子项占用一定的空间。这有助于layout在不同的屏幕尺寸和分辨率上看起来相似。

所以,例如......

<TextView 
    android:text="Some Text here"
    android:layout_weight="1"
    ...
LinearLayout
    android:id="@+id/image_layout"
    android:layout_weight="3"
    ...
<LinearLayout
    android:id="@+id/button_layout1"
    android:layout_weight="2"
    ...
<LinearLayout
    android:id="@+id/button_layout2"
    android:layout_weight="2"
    ...

您可能需要将weights调整为您想要/需要的东西,但这样的东西应该会有所帮助。您还需要将每个height设置为0dp,因为父LinearLayout是垂直的。