当另一个按钮消失时,线性布局中的Android一个按钮会消失。

问题描述:

我有一个线性布局的3个元素,从左到右依次是按钮和回收列表视图和第二个按钮。 我设置了线性布局下的第三个按钮(“消失按钮”),通过将第一个按钮设置为消失,使第一个按钮消失。 (第三个按钮仅用于测试,但最终我希望回收站视图的状态发送此消息。) 当我按下“消失按钮”时,第二个按钮也消失了,并且回收站接管了分配给线性组件的整个空间布局。 我担心我对android系统的工作原理有一些基本的误解,因为我以前也遇到类似的问题。 如果您改变影响布局的事情,是否有必要以某种方式重新显示活动? 我的主要活动是这样的当另一个按钮消失时,线性布局中的Android一个按钮会消失。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:weightSum="7" 
     android:id="@+id/linearLayout"> 
     <Button 
      android:id="@+id/first" 
      android:text="First" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/rvNumbers" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:scrollbars="horizontal" 
      android:layout_weight="5"/> 
     <Button 
      android:text="Last" 
      android:layout_width="0dp" 
      android:layout_height="match_parent" 
      android:layout_weight="1"/> 

    </LinearLayout> 
    <Button 
     android:id="@+id/first_off" 
     android:text="turn off first" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/linearLayout" 
     android:layout_alignParentStart="true" 
     /> 
</RelativeLayout> 

和Java代码看起来像这样

package andre.recyclerview_9_20_2017; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class MainActivity extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     RecyclerView rvContacts = (RecyclerView) findViewById(R.id.rvNumbers); 
     // Create adapter passing in the sample user data 
     NumberssAdapter adapter = new NumberssAdapter(this, 4000); 
     // Attach the adapter to the recyclerview to populate items 
     rvContacts.setAdapter(adapter); 
     // Set layout manager to position the items 
     LinearLayoutManager layoutManager = new LinearLayoutManager(this, 
       LinearLayoutManager.HORIZONTAL, false); 
     rvContacts.setLayoutManager(layoutManager); 

     Button button = (Button)findViewById(R.id.first_off); 
     button.setOnClickListener(new View.OnClickListener(){ 
      public void onClick(View v){ 
       firstOff(); 
      } 
     }); 

    } 

    void firstOff(){ 
     Button button = (Button)findViewById(R.id.first); 
     button.setVisibility(View.GONE); 
     RecyclerView RecyclerView = (RecyclerView)findViewById(R.id.rvNumbers); 
     LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
     //p.weight = 6; 
     RecyclerView.setLayoutParams(p); 
    } 
} 

note I have left out the code for the recycler view 
my build.gradle looks like this 
apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.3" 
    defaultConfig { 
     applicationId "andre.recyclerview_9_20_2017" 
     minSdkVersion 23 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.android.support:recyclerview-v7:25.3.1' 
    compile 'com.android.support.constraint:constraint-layout:1.0.2' 
    testCompile 'junit:junit:4.12' 
} 

不是100%确定你想要什么achive但尝试按钮设置为不可见使用button.setVisibility(查看。 INVISIBLE);,但使用View.GONE该按钮将从视图中移除。

您可以android:layout_alignWithParentIfMissing

从技术文档尝试:

如果设置为true,则父将作为锚时,锚 不能被用于layout_toLeftOf,layout_toRightOf等发现