2016-03-03 2 views
0

Я пытаюсь сделать приложение, но когда я тестирую его на разных устройствах, кнопка остается того же размера. Я попытался использовать Wrap_Content с дополнением, равным 5dp. Вот код:Как сделать кнопку подходящей для экрана?

<?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="match_parent" 
android:background="#000"> 

<FrameLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <ImageView 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:id="@ id/img2" 


     /> 

    <Button 
     android:layout_width="192dp" 
     android:layout_height="wrap_content" 
     android:id="@ id/button" 
     android:text="wallpaper" 
     android:alpha="0.1" 
     android:layout_gravity="left|bottom" 
     /> 

    <Button 
     android:layout_width="192dp" 
     android:layout_height="wrap_content" 
     android:text="back" 
     android:id="@ id/button2" 
     android:layout_gravity="right|bottom" 
     android:alpha="0.1" /> 



</FrameLayout> 

Я хочу, чтобы обе кнопки, чтобы встретиться в центре нижней, но не как ожидалось для всех устройств. Вот что происходит, когда я установил Match_Parent

+0

что именно вы хотите? –

ответ

1

Если вы хотите кнопки, чтобы быть бок о бок, и подходят для всех размеров экрана и ориентации , вы не можете выполнить его с весами на линейной компоновке.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayoutxmlns:android=" http://schemas.android.com/apk/res /android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="#000"> 
<FrameLayout android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@ id/img2"/> 
<LinearLayout android:width="match_parent" 
android:height="wrap_content" 
android:orientation="horizontal" 
android:layout_gravity="bottom"> 
<Button android:layout_width="0dp" 
android:layout_weight="0.5" 
android:layout_height="wrap_content" 
android:id="@ id/button" 
android:text="wallpaper" 
android:alpha="0.1"/> 

<Button android:layout_width="0dp" 
android:layout_weight="0.5" 
android:layout_height="wrap_content" 
android:text="back" 
android:id="@ id/button2" 
android:alpha="0.1"/> 
</LinearLayout> 
</FrameLayout> 

не пугайтесь с размером 0dp. Атрибут weight используется для распределения размера на основе отношения. Здесь обе кнопки имеют 0,5 вес. Таким образом, оба одинаково занимают всю ширину. Поскольку ширина вычисляется, нет необходимости указывать размер. Поэтому он установлен в 0.

+0

thx всего за помощь –

+0

Его мое удовольствие помочь. Надеюсь, это то, чего вы ожидали. –

+0

Я тоже искал что-то, чтобы удалить пробелы, и он отлично сработал. – Andrain

0

Используйте это:

<?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="match_parent" 
    android:background="#000"> 

    <FrameLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <ImageView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:id="@+id/img2" /> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 
       <Button 
        android:layout_weight="1" <!-- add weight --> 
        android:layout_width="0dp" <!-- change 192dp to 0dp --> 
        android:layout_height="wrap_content" 
        android:id="@+id/button" 
        android:text="wallpaper" 
        android:alpha="0.1" 
        android:layout_gravity="left|bottom" /> 
       <Button 
        android:layout_weight="1" <!-- add weight --> 
        android:layout_width="0dp" <!-- change 192dp to 0dp --> 
        android:layout_height="wrap_content" 
        android:id="@+id/button2" 
        android:text="back" 
        android:alpha="0.1" 
        android:layout_gravity="left|bottom" /> 
     </LinearLayout> 
    </FrameLayout> 
</RelativeLayout> 
+0

показывает ошибку, поскольку атрибуты должны быть определены как для высоты, так и для ширины. –

+0

Исправлено. Проверь сейчас. –

0

Попробуйте этот код -

<?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="match_parent" 
android:background="#000"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal"> 

    <Button 
     style="@style/Widget.AppCompat.Button.Borderless" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:id="@+id/button" 
     android:text="wallpaper" 
     android:alpha="0.1" /> 

    <View 
     android:layout_width="1dp" 
     android:layout_height="match_parent" 
     android:background="#fff" /> 

    <Button 
     style="@style/Widget.AppCompat.Button.Borderless" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:text="back" 
     android:id="@+id/button2" 
     android:alpha="0.1" /> 

</LinearLayout> 
</RelativeLayout>