如何创建具有角度的设置按钮以及如何在相对布局中设置按钮图像

时间:2014-05-01 06:57:52

标签: android android-layout

<?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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/contact_relMainHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#16A180" >

        <Button
            android:id="@+id/contact_btnMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:background="@drawable/scrol"
             />

        <Button
            android:id="@+id/contact_btnLogout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/lock" />
         <TextView
            android:id="@+id/textView"
            android:layout_width="100dp"
            android:layout_height="48dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/centertext"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/contact_linearHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_relMainHeader" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ee6958"
            android:gravity="center"
            android:padding="5dp"
            android:text="Contacts"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ee6958"
            android:gravity="center"
            android:padding="5dp"
            android:text="Map"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/contact_relSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_linearHeader"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/contact_editSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/contact_btnSearch" />

        <Button
            android:id="@+id/contact_btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

    <ListView
        android:id="@+id/contact_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/contact_relSearch" >
    </ListView>

</RelativeLayout>

这个我的Xml可以显示像这样的布局Link1我无法做我将如何使联系按钮角度形状Link2像给定的图像我将如何设置滑动菜单按钮,Loci图像和注销按钮。请帮我发布布局xml文件。

3 个答案:

答案 0 :(得分:0)

首先,在顶部布局中留出一些空间,其中包含滑动菜单按钮,Loci图像和注销按钮,您需要将填充设置为android:id="@+id/contact_relMainHeader"

要显示联系人按钮下方的角度箭头,您需要在单独布局内的按钮下方添加imageview。

查看以下代码。

<?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:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/contact_relMainHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#16A180"
        android:padding="5dp" >

        <ImageView
            android:id="@+id/contact_btnMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerInParent="true"
            android:background="@drawable/scrol" />

        <Button
            android:id="@+id/contact_btnLogout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:background="@drawable/lock" />

        <ImageView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:background="@drawable/centertext"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>

    <LinearLayout
        android:id="@+id/contact_linearHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/contact_relMainHeader" >

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ee6958"
            android:gravity="center"
            android:padding="5dp"
            android:text="Contacts"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold" />

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#ee6958"
            android:gravity="center"
            android:padding="5dp"
            android:text="Map"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/nl_rllayutbelow"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/contact_linearHeader"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/nl_ivNotifArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/down_arrow" /> //Set the arrow image of your angle.

        <ImageView
            android:id="@+id/nl_ivMesgArrow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:src="@drawable/down_arrow" />
    </LinearLayout>

    <RelativeLayout
        android:id="@+id/contact_relSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/nl_rllayutbelow"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/contact_editSearch"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/contact_btnSearch" />

        <Button
            android:id="@+id/contact_btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />
    </RelativeLayout>

    <ListView
        android:id="@+id/contact_listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/contact_relSearch" >
    </ListView>

</RelativeLayout>

<强>编辑:

为您的搜索框试用以下代码。

<RelativeLayout
    android:id="@+id/contact_relSearch"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nl_rllayutbelow"
    android:layout_margin="10dp"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/contact_editSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableLeft="@android:drawable/ic_search_category_default" 
        android:drawableRight="@drawable/delete" //Set drop arrow image.
        android:layout_toLeftOf="@+id/contact_btnSearch" />

    <ImageView
        android:id="@+id/contact_btnSearch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/delete"
        android:layout_alignParentRight="true" />
</RelativeLayout>

对于联系人和地图之间的分区,请在View之间保留以下内容:

      <LinearLayout
    android:id="@+id/contact_linearHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/contact_relMainHeader" >

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ee6958"
        android:gravity="center"
        android:padding="5dp"
        android:text="Contacts"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:textStyle="bold" />

    <View
        android:id="@+id/view1"
        android:layout_width="1dp"
        android:layout_height="10dp"
        />

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#ee6958"
        android:gravity="center"
        android:padding="5dp"
        android:text="Map"
        android:textColor="#ffffff"
        android:textSize="20dp"
        android:textStyle="bold" />
</LinearLayout>

答案 1 :(得分:0)

Genius尝试按钮角度..

  1. 可绘制资源
  2. button_pressed.png

    enter image description here

    button_normal.png

    enter image description here

    2的 MainActivity.java

       public class MainActivity extends ActionBarActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new   PlaceholderFragment()).commit();
        }
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
    
        /**
         * Declaration.
         */
        private Button contact, map;
        private TextView taskText;
    
        public PlaceholderFragment() {
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            /**
             * Initialize.
             */
            contact = (Button) rootView.findViewById(R.id.contactBtn);
            map = (Button) rootView.findViewById(R.id.mapBtn);
            taskText = (TextView) rootView.findViewById(R.id.taskText);
    
            return rootView;
        }
    
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
    
            contact.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    contact.setBackgroundResource(R.drawable.pressed);
                    taskText.setText("Contact list task");
                    map.setBackgroundResource(R.drawable.normal);
                }
            });
            map.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    map.setBackgroundResource(R.drawable.pressed);
                    taskText.setText("Map View task");
                    contact.setBackgroundResource(R.drawable.normal);
                }
            });
        }
    }
    
     }
    

    3的 fragment_main.xml

    <LinearLayout 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:orientation="vertical"
    tools:context="com.silambarasan.stackoverflow.MainActivity$PlaceholderFragment" >
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp" >
    
        <Button
            android:id="@+id/mapBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="2dp"
            android:background="@drawable/normal"
            android:text="Map"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/contactBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/mapBtn"
            android:layout_centerVertical="true"
            android:layout_marginRight="2dp"
            android:background="@drawable/normal"
            android:text="Contacts"
            android:textColor="#FFFFFF" />
    
        <Button
            android:id="@+id/button1"
            android:layout_width="1dp"
            android:layout_height="50dp"
            android:layout_alignBaseline="@+id/mapBtn"
            android:layout_alignBottom="@+id/mapBtn"
            android:layout_centerHorizontal="true"
            android:background="#FFFFFF" />
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="50dp" >
    
        <TextView
            android:id="@+id/taskText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </RelativeLayout>
    
    </LinearLayout>
    
    1. 结果

      enter image description here enter image description here

答案 2 :(得分:0)

// try this way, hope this will help you...
<?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" >

    <LinearLayout
        android:id="@+id/lnrFirstHeader"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="5dp"
        android:background="#16A180" >

        <ImageView
            android:id="@+id/imgContactMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/> // here set you header left menu image

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:layout_weight="1">
            <ImageView
                android:id="@+id/imgHeaderLogo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/ic_launcher"/> // here set you header center logo image
        </LinearLayout>
        <ImageView
            android:id="@+id/imgLogoutMenu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher"/> // here set you header right logout image
    </LinearLayout>

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:id="@+id/lnrContacts"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:background="#ee6958">
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Contacts"
                    android:textColor="#ffffff"
                    android:textSize="20dp"
                    android:textStyle="bold"/>
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lnrMap"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:background="#ee6958">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="center"
                    android:padding="5dp"
                    android:text="Map"
                    android:textColor="#ffffff"
                    android:textSize="20dp"
                    android:textStyle="bold" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_launcher"/> // here set your arrow image
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center">
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:adjustViewBounds="true"
                    android:src="@drawable/ic_launcher"/> // here set your arrow image
            </LinearLayout>
        </LinearLayout>

    </LinearLayout>


    <EditText
        android:id="@+id/edtSearch"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:drawableRight="@drawable/ic_launcher"
        android:layout_marginTop="5dp"/> // here set your EditText right side image

   <LinearLayout
    android:id="@+id/lnrFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_marginTop="5dp"
    android:layout_weight="1">

    // run time you have add respective fragment on second header item click like contact list and map.

   </LinearLayout>

</LinearLayout>