Android 相对长度不为';行不通
有以下代码:Android 相对长度不为';行不通,android,Android,有以下代码: <LinearLayout android:layout_width="match_parent" android:layout_height="0dip" android:layout_weight="0.9" android:gravity="center" android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationYesterdayValueDisplay"
android:layout_width="15dip"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationYesterdayValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:max="10"
android:progress="0" />
</LinearLayout>
现在我需要将LinearLayout更改为RelativeLayout。如果我这样做了,那么整个屏幕上都会出现相对延迟,这很糟糕。我知道RelativeLayout的工作不使用布局权重。请告诉我,我怎样才能修好它?我曾尝试为RelativeLayout制作LinearLayout包装器,但没有帮助
更新:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.6"
android:orientation="vertical" >
<RelativeLayout
android:background="#000000"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
</LinearLayout>
</LinearLayout>
UPDATE 2: FUll code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.5" >
<LinearLayout
android:weightSum="1.0"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9">
<TextView
android:id="@+id/activityEvaluationYesterdayValueDisplay"
android:layout_width="15dip"
android:layout_height="wrap_content"
android:text="0"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/activityEvaluationYesterdayValue"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationYesterdayValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:max="10"
android:progress="0" />
</RelativeLayout>
<TextView
android:id="@+id/TextView05"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Yesterday"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationTodayValueDisplay"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationTodayValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="10"
android:progress="0" />
</LinearLayout>
<TextView
android:id="@+id/TextView03"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Today"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationTomorrowValueDisplay"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationTomorrowValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="10"
android:progress="0" />
</LinearLayout>
<TextView
android:id="@+id/TextView02"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Tomorrow"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="0.5" >
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationEnergyValueDisplay"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationEnergyValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="10"
android:progress="0" />
</LinearLayout>
<TextView
android:id="@+id/TextView11"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Energy"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationWeatherValueDisplay"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationWeatherValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="10"
android:progress="0" />
</LinearLayout>
<TextView
android:id="@+id/TextView09"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Weather"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.33"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.9"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="@+id/activityEvaluationHoursSleptValueDisplay"
android:layout_width="20dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="0"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<com.ulnda.mypsych.views.VerticalSeekBar
android:id="@+id/activityEvaluationHoursSleptValue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:max="10"
android:progress="0" />
</LinearLayout>
<TextView
android:id="@+id/TextView07"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.1"
android:gravity="center"
android:text="Hours Slept"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
</LinearLayout>
</TableRow>
</TableLayout>
</LinearLayout>
更新2:完整代码
为什么RelativeLayout填充整个屏幕,而不是0.6
android:layou weight
仅对LinearLayout
有意义。Lint应该通过一条警告提醒您:android:layout\u weight仅适用于线性布局
。Lint应该通过警告提醒您,RelativeLayout是唯一一个具有布局权重属性的孩子,因此您还应该指定权重总和:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.6">
</RelativeLayout>
</LinearLayout>
RelativeLayout
是唯一具有layout\u weight
属性的孩子,因此您还应该指定weightSum
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:background="#ff0000"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="0.6">
</RelativeLayout>
</LinearLayout>
但我如何完成任务?我需要使用RelativeLayout,以便将我的VerticalSeekBar按中心对齐,并将TextView按VerticalSeekBarRelativeLayout的左侧对齐。有很多方法可以放置东西。例如,我知道要查看文档,但我不能只使用RelativeLayout,因为它占据了所有屏幕空间。请告诉我,我如何修复它?它填满了屏幕空间,因为对于在其中声明的每个元素,您应该提供其位置,在RelativeLayout中没有任何内容。然后,与match_parent一起,您要求填充所有可用空间。也许你想尝试一下“包装内容”,但我该如何完成我的任务呢?我需要使用RelativeLayout,以便将我的VerticalSeekBar按中心对齐,并将TextView按VerticalSeekBarRelativeLayout的左侧对齐。有很多方法可以放置东西。例如,我知道要查看文档,但我不能只使用RelativeLayout,因为它占据了所有屏幕空间。请告诉我,我如何修复它?它填满了屏幕空间,因为对于在其中声明的每个元素,您应该提供其位置,在RelativeLayout中没有任何内容。然后,与match_parent一起,您要求填充所有可用空间。也许你想试试“包装内容”?谢谢!请告诉我,我在哪里可以读到有关weightSum的信息?我对此一无所知。如果您的第一个代码适用于LinearLayout
,那么它也应该适用于RelativeLayout
。您可以在这里阅读有关权重总和的信息,请看我的第二次更新。此代码无法正常工作。我刚刚将LinearLayout更改为RelativeLayout,并为RelativeLayout的childs添加了一些属性。请看这个,谢谢!请告诉我,我在哪里可以读到有关weightSum的信息?我对此一无所知。如果您的第一个代码适用于LinearLayout
,那么它也应该适用于RelativeLayout
。您可以在这里阅读有关权重总和的信息,请看我的第二次更新。此代码无法正常工作。我刚刚将LinearLayout更改为RelativeLayout,并为RelativeLayout的childs添加了一些属性。请看这个。