【发布时间】:2016-09-22 12:33:58
【问题描述】:
我想创建一个有四个按钮的屏幕,每个按钮都是菱形的(就像一个向一侧转 45 度的正方形),所有四个按钮都排列成一个更大的菱形。
我在这里搜索了 SO 并设法创建了这个 xml 文件,它暗示了我想要实现的目标:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/White"
android:orientation="vertical"
android:visibility="visible">
<TextView
android:id="@+id/scoreCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge">
</TextView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_weight="1.0" >
<Button
android:id="@+id/topLeftOuter"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height = "60dp"
android:background="@color/Blue"
android:clickable="true"
android:visibility="visible"
android:adjustViewBounds="true"
android:rotation="45" >
</Button>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_weight="1.0" >
<Button
android:id="@+id/bottomLeftOuter"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height = "60dp"
android:background="@color/Yellow"
android:clickable="true"
android:visibility="visible"
android:adjustViewBounds="true"
android:rotation="45" >
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_weight="1.0" >
<Button
android:id="@+id/bottomRightOuter"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height = "60dp"
android:background="@color/Red"
android:clickable="true"
android:visibility="visible"
android:adjustViewBounds="true"
android:rotation="45" >
</Button>
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:clickable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:layout_weight="1.0" >
<Button
android:id="@+id/topRightOuter"
android:layout_centerInParent="true"
android:layout_width="60dp"
android:layout_height = "60dp"
android:background="@color/Chartreuse"
android:clickable="true"
android:visibility="visible"
android:adjustViewBounds="true"
android:rotation="45" >
</Button>
</RelativeLayout>
</LinearLayout>
这是结果:
但我想要更多类似的东西:
我应该怎么去那里?
【问题讨论】:
-
您无法使用线性布局来实现它。使用相对布局。
-
当您使用垂直线性布局的权重时,
height必须是0dp,如果是水平布局,那么width必须是0dp。父布局也有你的子视图的权重总和。
标签: java android android-layout