2017-09-26 14 views
0

私は、ボタンとリサイクラーのリストビューと2番目のボタンの左から右へ、線形レイアウトで3つの要素を持っています。 最初のボタンが消えるように設定されている線形レイアウトの下に3番目のボタン(「消しボタン」)があります。 (この3番目のボタンはテスト用ですが、最終的にリサイクラビューの状態でこのメッセージを送信します) "消しボタン"を押すと、2番目のボタンも消え、リサイクラはリニアに割り当てられたスペース全体を引き継ぎますレイアウト。 私は過去に同様の問題を抱えていたので、アンドロイドシステムがどのように機能するのかという基本的な誤解があることを恐れています。 レイアウトに影響を与えるものを変更すると、何とかそのアクティビティを再表示する必要がありますか? 私の主な活動は、この他のボタンが消えてしまえば、線形レイアウトのAndroid oneボタンが消えます。

<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' 
} 

答えて

0

ようにあなたがachive欲しいものを100%確実ではないに見えますがbutton.setVisibility(ビューを使用して目に見えないにボタンを設定してみてください。 INVISIBLE);ただし、View.GONEを使用すると、ボタンはビューから削除されます。

0

あなたはドキュメントからandroid:layout_alignWithParentIfMissing

で試すことができます:trueに設定した場合、親はアンカーとして使用されます

アンカー がなどlayout_toLeftOf、layout_toRightOf、のために見つけることができません

関連する問題