如何在xml(Android)

时间:2015-08-05 15:06:13

标签: java android xml

我正在尝试使用android中的xml创建下面的视图。我尝试用Padding(使用dp)创建它,但它并不适合所有屏幕分辨率。我该怎么办?

我的xml代码:(更新)

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res    /android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/eee"
    tools:context=".MainActivity"
    android:id="@+id/eee">

    <include layout="@layout/buttons" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="9"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            android:layout_marginBottom="0dp"
            android:layout_weight="8"
            android:gravity="center_vertical"

            >

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/eee"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:background="@drawable/eeee"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/eee"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:background="@drawable/eee"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

            <Button
                android:id="@+id/eeee"
                android:layout_weight="2"
                android:layout_width="90dp"
                android:layout_height="90dp"
                android:paddingLeft="10dp"
                android:paddingRight="10dp"
                android:background="@drawable/drawabke"

                />

            <View
                android:layout_width="0px"
                android:layout_height="match_parent"
                android:layout_weight="15" />

        </LinearLayout>

        <View
            android:id="@+id/idd"
            android:layout_width="match_parent"
            android:layout_height="410dp"
            android:layout_alignParentBottom="true"

            android:layout_weight="2"/>


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:layout_centerHorizontal="true"
            android:text="TextHere"
            android:layout_alignParentBottom="true"
            />

    </LinearLayout>


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:layout_centerHorizontal="true"
        android:text="TxetHere"
        android:layout_alignParentBottom="true"
        android:textColor="#f7f7f7"
        />

    <com.lorentzos.flingswipe.SwipeFlingAdapterView
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:rotation_degrees="15.5"
        android:paddingTop ="100dp"
        tools:context=".MyActivity" />


</RelativeLayout>

This is what I want

3 个答案:

答案 0 :(得分:1)

尝试这样的事情:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content" />

</Linear>

这里是你在LinearLayout中有三个具有相同layout_weight的视图。

编辑:为了没有拉链的按钮你可以用LinearLayout包装每个Button,如下所示:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />

    </LinearLayout>

[...]

</Linear>

答案 1 :(得分:0)

如果没有看到您正在使用的xml或者实际设备的屏幕截图,很难说出您在做什么,但通常情况下,要调整元素大小以均匀分享您想要的空间将它们的宽度/高度设置为0或&#34; wrap_content&#34;然后设置&#34; layout_weight&#34;所有元素的数字相同。

答案 2 :(得分:0)

检查一下,这正是你想要的:

<LinearLayout
     android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="0dp"
android:layout_weight="5"
android:gravity="center_vertical"
android:orientation="horizontal" >


<Button
android:id="@+id/eee"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:layout_marginRight="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />


<Button
android:id="@+id/eef"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"/>


<Button
android:id="@+id/eeee"
android:layout_width="0dp"
android:layout_height="90dp"
android:layout_weight="1"
android:background="@drawable/ic_launcher"
android:layout_marginLeft="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp" />

</LinearLayout>