android layout_weight 与 RelativeLayout
android layout_weight with RelativeLayout
我是 android 的新手,遇到以下问题:
我想将我的屏幕拆分为 4 个线性布局,我需要根布局是相对布局,
我尝试使用 layout_weight 属性 以便在屏幕上平均分割我的 4 布局,但我只有在将根布局用作线性布局时才成功地做到这一点。
布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">
<LinearLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="B status"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="D status"/>
</LinearLayout>
</LinearLayout>
(^ 我最后关闭了 'RelativeLayout' 标签,由于某些原因它没有显示)
This is the screen when the root layout is LinearLayout
And this is when it's RelativeLayout(如上xml)
我可以使用 'dp' 单位将屏幕分成 4 个,但这样我在其他方面遇到了问题...
我的主要目标是能够将浮动图片从一个布局拖放到另一个布局,为此我需要使用 relativeLayout,另外我想通过使用 Rect 属性知道图像被拖放到哪个布局上由于某种原因错误的位置。
非常感谢! :)
只需在 RelativeLayout 的中间添加一个 Textview,父级属性为 center,top 将在该 textView 上方,LinearLayout 将在该 Textview 下方,您可以使该 textview 透明或您选择的任何内容(我做了你提到的相同颜色)见下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">
<LinearLayout
android:id="@+id/top_layout"
android:layout_above="@+id/tv_dummy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="A status" />
</LinearLayout>
<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="B status" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tv_dummy"
android:layout_centerInParent="true"
android:background="#5080ce"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_below="@+id/tv_dummy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="C status" />
</LinearLayout>
<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="D status" />
</LinearLayout>
</LinearLayout>
我是 android 的新手,遇到以下问题: 我想将我的屏幕拆分为 4 个线性布局,我需要根布局是相对布局, 我尝试使用 layout_weight 属性 以便在屏幕上平均分割我的 4 布局,但我只有在将根布局用作线性布局时才成功地做到这一点。 布局xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">
<LinearLayout
android:id="@+id/top_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="B status"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="C status"/>
</LinearLayout>
<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="D status"/>
</LinearLayout>
</LinearLayout>
(^ 我最后关闭了 'RelativeLayout' 标签,由于某些原因它没有显示)
This is the screen when the root layout is LinearLayout
And this is when it's RelativeLayout(如上xml)
我可以使用 'dp' 单位将屏幕分成 4 个,但这样我在其他方面遇到了问题... 我的主要目标是能够将浮动图片从一个布局拖放到另一个布局,为此我需要使用 relativeLayout,另外我想通过使用 Rect 属性知道图像被拖放到哪个布局上由于某种原因错误的位置。
非常感谢! :)
只需在 RelativeLayout 的中间添加一个 Textview,父级属性为 center,top 将在该 textView 上方,LinearLayout 将在该 Textview 下方,您可以使该 textview 透明或您选择的任何内容(我做了你提到的相同颜色)见下面的代码。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/view_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:splitMotionEvents="false">
<LinearLayout
android:id="@+id/top_layout"
android:layout_above="@+id/tv_dummy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/a_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#5080ce">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="A status" />
</LinearLayout>
<LinearLayout
android:id="@+id/b_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#356dc6">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="B status" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/tv_dummy"
android:layout_centerInParent="true"
android:background="#5080ce"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/bottom_layout"
android:layout_below="@+id/tv_dummy"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:id="@+id/c_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#325287">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="C status" />
</LinearLayout>
<LinearLayout
android:id="@+id/d_status_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#26477c">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="D status" />
</LinearLayout>
</LinearLayout>