2015-02-03 5 views
1

Dialog을 대화 상자 내에 사용자 정의 객체가있는 ListView을 표시 할 수있는 사용자 정의 레이아웃으로 만들었습니다. 또한 DialogToolbar (android.support.v7.widget.Toolbar)을 추가하여 좀 더 물질적 인 느낌을주었습니다. Toolbar 제목을 설정하는 방법을 알아 내려고했지만 몇 가지 문제가있었습니다. 보통 나는 다음과 같은 일을 할 것입니다 :대화 상자에서 툴바 제목 설정

myToolbar = (Toolbar) findViewById(R.id.my_toolbar);    
setSupportActionBar(myToolbar); 

그러나, 나는 내가 setSupportActionBarActionBarActivity 방법이다 Dialog 때문에 구축 생성 한 클래스에서 이것을 사용할 수 없습니다. 또한 setTitle을 사용하여 제목 텍스트를 지정하려고했지만 아무 소용이 없습니다.

따라서 ActionBarActivity 방법을 사용하지 않고 Toolbar 제목을 설정하는 가장 좋은 방법은 무엇입니까?

예를 들어
public class ListGroup 
{ 
    public static Dialog CreateGroupDialog(Context context, ArrayList<ListObject> arrayList) 
    { 
     final Dialog dialog = new Dialog(context, R.style.dialogTheme); 
     dialog.setContentView(R.layout.group_dialog); 
     Button dialogButton = (Button)dialog.findViewById(R.id.dialog_ok_button); 
     dialogButton.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       dialog.dismiss(); 
      } 
     }); 

     LayoutInflater li = LayoutInflater.from(context); 
     View view = li.inflate(R.layout.group_dialog, null); 
     Toolbar toolbar = (Toolbar) view.findViewById(R.id.dialog_toolbar); 
     // The following line has no effect... 
     toolbar.setTitle("List Group"); 

     dialog.setCancelable(false); 
     ListView listView = (ListView) dialog.findViewById(R.id.group_listview); 
     CustomAdapter customAdapter = new CustomAdapter(context, R.layout.list_object, arrayList); 
     listView.setAdapter(customAdapter); 
     return dialog; 
    } 
} 

, 나는 다음 그래서 같은 Dialog을 만들 것

Dialog 레이아웃

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/dialog_toolbar" 
     android:layout_height="52dp" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" /> 

    <ListView 
     android:id="@+id/group_listview" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:paddingTop="5dp" 
     android:cacheColorHint="@android:color/transparent" 
     android:choiceMode="multipleChoice" 
     android:layout_weight="1" 
     android:isScrollContainer="false" 
     android:divider="@color/grey_300" 
     android:dividerHeight="1dip" 
     android:fadingEdge="none"> 
    </ListView> 

    <LinearLayout 
     android:id="@+id/btnHolderLL" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/dialog_ok_button" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:paddingRight="1dp" 
      android:paddingLeft="1dp" 
      android:textSize="17dp" 
      android:textColor="#FFFFFF" 
      android:textAllCaps="false" 
      android:background="@drawable/button_selector" 
      android:text="Ok" 
      android:clickable="true"/> 
    </LinearLayout> 

</LinearLayout> 

Dialog 빌더 클래스 : 내 코드는 다음과 같습니다

ArrayList arrayList = new ArrayList<ListObject>(); 
ListObject listObject1 = new ListObject("Item #1", new String[]{""}, "", "", "", "blank", 
     "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null); 
ListObject listObject2 = new ListObject("Item #2", new String[]{""}, "", "", "", "blank", 
     "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null); 
ListObject listObject3 = new ListObject("Item #3", new String[]{""}, "", "", "", "blank", 
     "blank", "blank", "blank", "blank", "blank", "", 1, false, 0, 0, false, false, null, null); 
arrayList.add(listObject1); 
arrayList.add(listObject2); 
arrayList.add(listObject3); 
Dialog groupDialog = ListGroup.CreateGroupDialog(MainActivity.this, null, arrayList); 
groupDialog.show(); 

그러면 아래와 같이 Dialog이 생성됩니다. 당신이 볼 수 있듯이, 당신은 당신이 새로운 레이아웃을 팽창하고 새로운 Toolbar를 참조하기보다는 하나에게 대화 상자를 사용하는

LayoutInflater li = LayoutInflater.from(context); 
View view = li.inflate(R.layout.group_dialog, null); 
Toolbar toolbar = (Toolbar) view.findViewById(R.id.dialog_toolbar); 

를 사용하는 경우 ...

enter image description here

답변

1

에는 제목이 없다 (dialog.setContentView(R.layout.group_dialog);에서 비정상적으로 사용함). 대화 상자에서 Button에 대한 참조를 얻은 방법과 마찬가지로 다음을 사용하여 툴바를 검색해야합니다.

Toolbar toolbar = (Toolbar) dialog.findViewById(R.id.dialog_toolbar); 
+0

내 부분에 대한 감독. 고마워,이게 내가 필요한거야. – Willis