2016-12-29 72 views
0

我的问题的元素是我要保持3个元素,如按钮比如我使用了一个RelativeLayout作为家长和LinearLayout,因为它的孩子,给android:weightSum="3"并添加3个按键,因为它的孩子。我已经实现了我想要的用户界面。 但是这里出现原始问题当按钮可见性状态之一消失了。元素不正确。对齐使用相对布局

即使当我隐藏一个按钮,其他两个按钮需要调整时,我该如何实现。

请提供解决方案,如果您有。

first image

secondimage

Respective Code 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 
<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:layout_alignParentBottom="true" 
android:orientation="horizontal" 
android:weightSum="3.0"> 
<Button 
       android:id="@+id/btn1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_weight="1.0" 
       android:textColor="#ffffff" 
       android:text="TAB1" /> 
      <Button 
       android:id="@+id/btn2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:text="TAB 2" 
       android:visibility="gone" /> 
      <Button 
       android:id="@+id/btn3" 
       android:layout_width="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_height="wrap_content" 
       android:layout_weight="1.0" 
       android:text="TAB 3" /> 
     </LinearLayout> 
    </RelativeLayout> 
</LinearLayout> 
+0

发表您的布局 –

+0

安置自己隐藏一个按钮 –

+2

集可视性,之后隐形UI,使这个问题得到解决。如果你设置了Gone,那么它会删除那个空间,但是如果你设置了Invisible,那么它将只隐藏UI,但是设置空间为你设置的重量 例如。 texview.setVisibility(查看。无形); –

回答

0

您不必设置android:weightSum属性,只是分配android:layout_weight=1给每一个孩子。

https://developer.android.com/guide/topics/ui/layout/linear.html

为了创建在其中每个子使用在屏幕上的空间中的相同量的线性布局,设定机器人:layout_height每个视图“0dp”(用于垂直布局)或的android:layout_width将每个视图设置为“0dp”(对于水平布局)。然后将每个视图的android:layout_weight设置为“1”。

如果您想要定义最大权重和,请使用android:weightSum如果未指定,则通过添加所有孩子的layout_weight来计算总和。

代码示例:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="horizontal"> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:visibility="gone"/> 
     <Button 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 
</LinearLayout> 
0

你必须使用的,而不是线性布局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:weightSum="3"> 
<Button 
    android:id="@+id/btnOne" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_weight="1" 
    android:text="1"/> 

<Button 
    android:id="@+id/btnTwo" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true"   
    android:layout_weight="1" 
    android:text="2"/> 
<Button 
    android:id="@+id/btnThree" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_weight="1" 
    android:text="3"/> 

相对布局,如果你设置可见走了第二个按钮,然后就可以实现这一目标

0

或者您可以通过programetically删除文本第二按钮和用于此设置透明单位的背景下,如以下代码

<?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="wrap_content" 
      android:orientation="horizontal" 
      android:weightSum="3"> 
<Button 
    android:id="@+id/btnOne" 
    android:layout_width="0dp" 
    android:text="1" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 

<Button   
    android:id="@+id/btnTwo" 
    android:background="@android:color/transparent" 
    android:layout_width="0dp"   
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 
<Button 
    android:text="3" 
    android:id="@+id/btnThree" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1"/> 

0

机器人:能见度=“隐形”设置中间视图此属性。 并确保您的子视图(即按钮)宽度设置为0dp。因为你正在使用重量来表示宽度。

尝试这个 -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/activity_splash" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@android:color/white" 
    tools:context="com.augmented.wiki.SplashActivity"> 

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

     <Button 
      android:id="@+id/activity_splash_btnImage" 
      android:layout_width="0dp" 
      android:text="Button 1" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" /> 

     <Button 
      android:id="@+id/activity_splash_btnImage1" 
      android:layout_width="0dp" 
      android:text="Button 2" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:visibility="invisible" /> 

     <Button 
      android:id="@+id/activity_splash_btnImage2" 
      android:layout_width="0dp" 
      android:text="Button 3" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

    </LinearLayout> 
</RelativeLayout>