Android 使用线性布局和权重更改可见性
因此,我有一个带有两个视图的Android 使用线性布局和权重更改可见性,android,android-layout,android-layout-weight,Android,Android Layout,Android Layout Weight,因此,我有一个带有两个视图的线性布局,我正在为每个视图设置权重 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/main_layout1" android:layout_width="match_parent" android:layou
线性布局
,我正在为每个视图设置权重
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="horizontal">
<com.sorted.view.SorteddTextView
android:id="@+id/percent1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="@dimen/normal"
android:layout_marginRight="@dimen/small"
android:layout_weight="2"
android:background="@color/sky_blue"
android:gravity="center"
android:text="0%"
android:textColor="@color/font_color"
android:textSize="@dimen/font_size_large"
android:visibility="invisible" />
<FrameLayout
android:id="@+id/frame1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8">
......
......
</FrameLayout>
</LinearLayout>
......
......
现在,首先我将可见性设置为GONE
from activity。然后在按下某个按钮时,我将可见性设置为VISIBLE
,但我无法看到视图。
但是,当可见性为不可见时,如果我将其更改为可见,则其工作正常
我认为,
LinearLayout
无法调整其视图(我也不知道)。如果您尝试将TableRow放置在桌面上
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center"
android:orientation="horizontal">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<com.sorted.view.SorteddTextView
android:id="@+id/percent1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="0%"
android:visibility="invisible" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="8">
<FrameLayout
android:id="@+id/frame1"
android:layout_width="0dp"
android:layout_height="wrap_content"
>
</FrameLayout>
</TableRow>
</LinearLayout>
您应该将
android:weightSum
添加到您的家长LinearLayout
<LinearLayout ...
android:id="@+id/main_layout1"
android:weightSum="10"
>
<com.sorted.view.SorteddTextView
android:id="@+id/percent1"
android:layout_weight="2"
/>
<FrameLayout
android:layout_weight="8">
...
</FrameLayout>
</LinearLayout>
这是因为当您将第一个
视图
设置为可见性消失时
,它会像预期的那样隐藏,并且空间由线性布局内的另一个视图
调整。这意味着所有可用空间现在都被第二个视图占用了。现在,当您再次使第一个视图
可见时
无法看到它,因为它与另一个视图重叠,而另一个视图已经占据了线性布局中的全部空间
解决此问题的方法是,将weightSum
分配给LinearLayout
,它将始终确保在weight=10
中,2代表第一个视图
,其余8代表第二个视图
。希望能有帮助
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center" android:weightSum="10"
android:orientation="horizontal">
问题出在哪里?简而言之,就是“可见不工作”。对可见对象不可见对视图工作正常已尝试此操作。它只是使框架布局位于中心。当SortedDdTextView不可见时,它工作正常。有问题GONE@SonuRaj我不知道你为什么会这样,但我曾经创建过一个演示,它可以工作。当我从“活动”更改可见性时,会发生这种情况。请尝试从“活动”更改可见性!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:gravity="center" android:weightSum="10"
android:orientation="horizontal">