2015-03-11 2 views
0

Я хочу создать custom title для Dialog:Возможно ли разместить компонент под другим внутри RelativeLayout?

public class MessageDialogView extends Builder { 

    private View view, titreView; 

    @SuppressLint("NewApi") 
    public MessageDialogView(Context context,LayoutInflater inflater) { 
     super(context); 
     view = inflater.inflate(R.layout.msg_dialog, null); 
     this.setView(view); 
     titreView = inflater.inflate(R.layout.custom_dialog_title, null); 
     this.setCustomTitle(titreView); 
    } 
    ... 
} 

макета пользовательского заголовка:

<?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="wrap_content" 
    android:orientation="horizontal" 
    style="@style/ImpotsTitleStyle" 
    android:layout_marginBottom="0dp" 
    android:padding="0dp"> 
    <ImageView 
     android:id="@+id/icone" 
     android:src="@drawable/ic_action_warning" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 
    <TextView 
     android:id="@+id/titre" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:layout_gravity="center_vertical" 
     style="@style/ImpotsStyleText"/> 
</LinearLayout> 

Во время выполнения текст заголовка не «по центру» в контексте всего всего заголовка, потому что есть значок слева от него!

enter image description here

Так что, если я поставил значок и текст заголовка в внутри RelativeLayout, можно поместить TextView под значком? Если да, то какой атрибут xml подходит для этого?

+0

Используйте 'FrameLayout' вместо' LinearLayout'. Дайте ImageView 'layout_gravity =" left "и TextView' 'layout_gravity =" center "' '. – Vikram

ответ

1

Да, a RelativeLayout является правильным решением. Вы можете выровнять ImageView влево, а затем центрироваться в TextView. Я изменил код, чтобы указать вам в правильном направлении.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    style="@style/ImpotsTitleStyle" 
    android:layout_marginBottom="0dp" 
    android:padding="0dp"> 

    <ImageView 
     android:id="@+id/icone" 
     android:src="@drawable/ic_action_warning" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true"/> 

    <TextView 
     android:id="@+id/titre" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     style="@style/ImpotsStyleText"/> 
</RelativeLayout> 
Смежные вопросы