2017-09-17 3 views
1

私のデザインには3つのボタンがあります。Android:Javaコードでlayout_constraintStart pointとlayout_marginを変更する

ボタン3は、条件によって表示または非表示にすることができます。

ここでは、3つのボタンが画面の中央に表示されます。そのうちの1つが見えるようになると、私はデザインを自動的に変更したいと思います。

さらに、アクティブバーマーカーがあります。このマーカーのconstraintStartを変更するには、ボタンをクリックします。事前に

おかげ

Screenshot

これは私のXMLです:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    android:orientation="vertical" 

    tools:layout_editor_absoluteX="0dp" 
    tools:layout_editor_absoluteY="25dp"> 


    <Button 
     android:id="@+id/btn1" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="100dp" 
     android:layout_height="40dp" 
     android:layout_marginStart="8dp" 
     android:text="Button1" 
     android:textSize="14sp" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="@+id/btn2" /> 

    <Button 
     android:id="@+id/btn2" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="100dp" 
     android:layout_height="40dp" 
     android:layout_marginStart="16dp" 
     android:text="Button2" 
     android:textSize="14sp" 
     app:layout_constraintBottom_toBottomOf="@+id/btn3" 
     app:layout_constraintStart_toEndOf="@+id/btn1" /> 

    <Button 
     android:id="@+id/btn3" 
     style="?android:attr/borderlessButtonStyle" 
     android:layout_width="120dp" 
     android:layout_height="40dp" 
     android:layout_marginRight="24dp" 
     android:layout_marginTop="16dp" 
     android:text="button3" 
     android:textSize="14sp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <ImageView 
     android:id="@+id/imgActiveBar" 
     android:layout_width="103dp" 
     android:layout_height="4dp" 
     android:layout_marginStart="8dp" 
     android:background="@android:color/holo_blue_dark" 
     android:scaleType="centerCrop" 
     app:layout_constraintStart_toStartOf="@+id/btn1" 
     app:layout_constraintTop_toBottomOf="@+id/btn1" /> 


</android.support.constraint.ConstraintLayout> 

これは私のJavaコードです:あなたはのstartToStartフィールドを変更する必要が

package au.com.test.listviewtest; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 

public class TestActivity extends AppCompatActivity { 
private boolean bCondition = false; 
    private Button btn1 = null; 
    private Button btn2 = null; 
    private Button btn3 = null; 
    private ImageView imgActiveBar = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_test); 

     btn1 = (Button) findViewById(R.id.btn1); 
     btn2 = (Button) findViewById(R.id.btn2); 
     btn3 = (Button) findViewById(R.id.btn3); 
     imgActiveBar = (ImageView) findViewById(R.id.imgActiveBar); 


     if (bCondition==true){ 
      btn3.setVisibility(View.INVISIBLE); 
      //If bt3 is invisible than 
      // make btn1 an btn2 centered 


     } else { 
      btn3.setVisibility(View.VISIBLE); 
      //If bt3 is visible than 
      // make btn1 an btn2 and bt3 centered 

     } 

     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
      // make ActiveBar's layout_constraintStart_toStartOf is bt1 
      } 
     }); 
     btn2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // make ActiveBar's layout_constraintStart_toStartOf is bt2 
      } 
     }); 
     btn3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       // make ActiveBar's layout_constraintStart_toStartOf is bt3 
      } 
     }); 


    } 
} 

答えて

1

imgActiveBarのレイアウトパラメータ:

ConstraintLayout.LayoutParams prams = (ConstraintLayout.LayoutParams) imgActiveBar.getLayoutParams(); 
prams.startToStart = btn1.getId(); //for example 
//you'll probably need to change the end position as well 
prams.endToEnd = btn1.getId(); 
imgActiveBar.setLayoutParams(prams); 
imgActiveBar.requestLayout(); 

この動作は、あなたが素晴らしいですTabLayout

+0

によって実装されます!これはまさに私が必要としていたものでした。どうもありがとう。 – MareCieco

関連する問題