2017-09-26 1 views
0

왼쪽부터 차례대로 단추와 재활용 목록보기 및 두 번째 단추가 선형 레이아웃에 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> 

처럼 보인다 그리고 자바 코드이

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 원하지만 눈에 보이지 않는 사용 button.setVisibility (보기에있는 버튼을 설정하려고 무엇을 100 % 확실처럼 보인다. INVISIBLE); 그러나 View.GONE을 사용하면 버튼이 뷰에서 제거됩니다.

0

당신은 문서에서 android:layout_alignWithParentIfMissing

으로 시도 할 수 있습니다 : true로 설정하면, 부모가 앵커로 사용됩니다

앵커 는 등 layout_toLeftOf, layout_toRightOf,로 발견 할 수 없다

관련 문제