2016-08-11 1 views
2

내 응용 프로그램에서 사용자 지정 작업 표시 줄을 사용하고 있습니다. 해당 작업 표시 줄에는 4 개의 아이콘과 1 개의 textview이 있습니다. 작업 표시 줄의 경우 선형 레이아웃을 사용하고 있습니다.사용자 지정 작업 표시 줄에서 다른 아이콘이나 텍스트보기에서 공간이 필요로하는 아이콘을 숨기면 작업 표시 줄

내 앱에 4 가지 활동이 있습니다. 각 활동마다 다른 작업 표시 줄이 있습니다. 첫 번째 활동을 열면 아이콘 하나가 사라집니다. 이러한 모든 활동에는 다른 아이콘이 있습니다.

질문 : 필자의 요구 사항은 아이콘이 textview에 의해 사용되어야 할 필요가없는 경우입니다. 나는 모든 것을 시도하지만 항상 스페이스는 액션 바를 끝낼 것입니다. 나는 스페이스를 원하지 않습니다.

여기 내 코드입니다.

MainActivity.class : -

public class MainActivity extends AppCompatActivity { 

    ImageView Image1,Image2,Image3,Image4; 
    TextView title; 
    Button btn1,btn2,btn3; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.content_main); 
     /*Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
*/ 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setCustomView(R.layout.custom_actionbar); 

     View view = getSupportActionBar().getCustomView(); 

     Image1=(ImageView)findViewById(R.id.cst_ok); 
     Image2=(ImageView)findViewById(R.id.cst_del); 
     Image3=(ImageView)findViewById(R.id.cst_edt); 
     Image4=(ImageView)findViewById(R.id.cst_srh); 
     title=(TextView)findViewById(R.id.cst_txt); 

     Image1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showdailog(); 
      } 
     }); 

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

     btn1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent1=new Intent(MainActivity.this,first_activity.class); 
       startActivity(intent1); 
      } 
     }); 

     btn2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent2=new Intent(MainActivity.this,second_activity.class); 
       startActivity(intent2); 
      } 
     }); 
     btn3.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent3=new Intent(MainActivity.this,third_activity.class); 
       startActivity(intent3); 
      } 
     }); 
private void showdailog() { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     final View dialogView = inflater.inflate(R.layout.custom_dialog, null); 
     dialogBuilder.setView(dialogView); 

     final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); 

     dialogBuilder.setTitle("Custom dialog"); 
     dialogBuilder.setMessage("Enter text below"); 
     dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

//    et1.setText(edt.getText()); 
       title.setText(edt.getText()); 

      } 
     }); 
     dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //pass 
      } 
     }); 
     AlertDialog b = dialogBuilder.create(); 
     b.show(); 
    } 
} 

FirstActivity.class : -

public class first_activity extends AppCompatActivity { 

    ImageView Image1,Image2,Image3,Image4; 
    TextView title; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment1); 

     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setCustomView(R.layout.custom_actionbar); 

     View view = getSupportActionBar().getCustomView(); 

     Image1=(ImageView)view.findViewById(R.id.cst_ok); 
     Image2=(ImageView)view.findViewById(R.id.cst_del); 
     Image3=(ImageView)view.findViewById(R.id.cst_edt); 
     Image4=(ImageView)view.findViewById(R.id.cst_srh); 
     title=(TextView)view.findViewById(R.id.cst_txt); 

     Image1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showdailog(); 
      } 
     }); 

     Image3.setVisibility(View.GONE); 
    } 

    private void showdailog() { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     final View dialogView = inflater.inflate(R.layout.custom_dialog, null); 
     dialogBuilder.setView(dialogView); 

     final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); 

     dialogBuilder.setTitle("Custom dialog"); 
     dialogBuilder.setMessage("Enter text below"); 
     dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

//    et1.setText(edt.getText()); 
       title.setText(edt.getText()); 

      } 
     }); 
     dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //pass 
      } 
     }); 
     AlertDialog b = dialogBuilder.create(); 
     b.show(); 
    } 
} 

SecondActivity.class : -

public class second_activity extends AppCompatActivity { 

    ImageView Image1,Image2,Image3,Image4; 
    TextView title; 

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

     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setCustomView(R.layout.custom_actionbar); 

     View view = getSupportActionBar().getCustomView(); 

     Image1=(ImageView)view.findViewById(R.id.cst_ok); 
     Image2=(ImageView)view.findViewById(R.id.cst_del); 
     Image3=(ImageView)view.findViewById(R.id.cst_edt); 
     Image4=(ImageView)view.findViewById(R.id.cst_srh); 
     title=(TextView)view.findViewById(R.id.cst_txt); 

     Image1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showdailog(); 
      } 
     }); 

     Image4.setVisibility(View.GONE); 
    } 

    private void showdailog() { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     final View dialogView = inflater.inflate(R.layout.custom_dialog, null); 
     dialogBuilder.setView(dialogView); 

     final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); 

     dialogBuilder.setTitle("Custom dialog"); 
     dialogBuilder.setMessage("Enter text below"); 
     dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

//    et1.setText(edt.getText()); 
       title.setText(edt.getText()); 

      } 
     }); 
     dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //pass 
      } 
     }); 
     AlertDialog b = dialogBuilder.create(); 
     b.show(); 
    } 
} 

ThirdActivity.class : -

public class third_activity extends AppCompatActivity { 

    ImageView Image1,Image2,Image3,Image4; 
    TextView title; 

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

     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setDisplayShowCustomEnabled(true); 
     getSupportActionBar().setCustomView(R.layout.custom_actionbar); 

     View view = getSupportActionBar().getCustomView(); 

     Image1=(ImageView)view.findViewById(R.id.cst_ok); 
     Image2=(ImageView)view.findViewById(R.id.cst_del); 
     Image3=(ImageView)view.findViewById(R.id.cst_edt); 
     Image4=(ImageView)view.findViewById(R.id.cst_srh); 
     title=(TextView)view.findViewById(R.id.cst_txt); 

     Image1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       showdailog(); 
      } 
     }); 

     Image2.setVisibility(View.GONE); 
    } 

    private void showdailog() { 
     AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this); 
     LayoutInflater inflater = this.getLayoutInflater(); 
     final View dialogView = inflater.inflate(R.layout.custom_dialog, null); 
     dialogBuilder.setView(dialogView); 

     final EditText edt = (EditText) dialogView.findViewById(R.id.edit1); 

     dialogBuilder.setTitle("Custom dialog"); 
     dialogBuilder.setMessage("Enter text below"); 
     dialogBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

//    et1.setText(edt.getText()); 
       title.setText(edt.getText()); 

      } 
     }); 
     dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 
       //pass 
      } 
     }); 
     AlertDialog b = dialogBuilder.create(); 
     b.show(); 
    } 
} 

Custom_Actionbar_layout : -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:weightSum="5"> 
<ImageView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:src="@drawable/ok" 
    android:id="@+id/cst_ok"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="Custom ActionBar" 
     android:id="@+id/cst_txt" 
     android:singleLine="false" 
     android:maxLines="2"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/delete1" 
     android:id="@+id/cst_del"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/edit" 
     android:id="@+id/cst_edt"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/search" 
     android:id="@+id/cst_srh"/> 
</LinearLayout> 

그리고 내 테마 <style name="AppTheme" parent="Theme.AppCompat.Light">

되고 텍스트보기 동적 텍스트를 변경 그것은 사용자가 주어진다 . 어느 것이 든 세부 사항을 필요로하면 나는 새롭게 할 것이다.

답변

0

당신은 당신의 LinearLayout5에의 android:weightSum 속성의 값을 설정하고 있습니다.

당신이 ( View.GONE에 가시성을 설정)이 레이아웃에서 View을 제거하고, 당신의 LinearLayoutweightSum은 여전히 ​​ 5이지만, 당신의 View의 실제 합계는 4, 따라서 빈 공간입니다.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" android:layout_width="match_parent" 
    android:layout_height="40dp"> 
<ImageView 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:src="@drawable/ok" 
    android:id="@+id/cst_ok"/> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:text="Custom ActionBar" 
     android:id="@+id/cst_txt" 
     android:singleLine="false" 
     android:maxLines="2"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/delete1" 
     android:id="@+id/cst_del"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/edit" 
     android:id="@+id/cst_edt"/> 
    <ImageView 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/search" 
     android:id="@+id/cst_srh"/> 
</LinearLayout> 
: 문제를 해결해야합니다 귀하의 LinearLayout에서 android:weightSum 속성을 제거

관련 문제