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

时间:2017-09-26 19:08:37

标签: android android-linearlayout relativelayout

我在线性布局中有3个元素,从左到右依次为按钮和回收列表视图,第二个按钮。 我在线性布局设置下面有第三个按钮(“消失按钮”),通过将第一个按钮设置为消失使第一个按钮消失。 (第三个按钮仅用于测试,但最后我希望回收站视图的状态发送此消息。) 当我按下“消失按钮”时,第二个按钮也会消失,并且回收者会占用分配给线性布局的整个空间。 我担心我对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'
}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您可以尝试使用android:layout_alignWithParentIfMissing

来自文档:

  

如果设置为true,则父级将用作锚点时的锚点   找不到layout_toLeftOf,layout_toRightOf等

相关问题