androidcssandroid-layoutandroid-activity

Button size according to background image


I am developing an application in which there are many buttons,what I want to do is make the size of button according to background image.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bac"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.ttttest.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="37dp"
    android:padding="4dip"
    android:text="Button" />

Solution

  • Interesting really. Never thought of this kind of design. Well I came up with a little something that is very close to what your looking for I believe. Aligning to a background image perfectly is not possible I believe.

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/white" >
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal"
        android:layout_height="wrap_content">
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
    
    </LinearLayout>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:layout_height="wrap_content">
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
        <Button
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="match_parent"/>
    
    </LinearLayout>