-1
我一直在使用按鈕的幾個圖像(下面的XML) - 但我需要按照程序創建按鈕的項目的一部分,該按鈕的格式不像這樣其他(我希望它是)。如何以編程方式使用Android按鈕XML文件
Buttonshape.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/button" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/button" />
<item android:drawable="@drawable/buttonpressed" />
</selector>
我的代碼,創建一個按鈕
Button newBut;
newBut = new Button(this);
newBut.setText("SOME TEXT");
newBut.setTextColor(Color.parseColor("#404040"));
newBut.setEllipsize(TruncateAt.MARQUEE);
newBut.setSingleLine();
newBut.setMarqueeRepeatLimit(50);
newBut.setSelected(true);
這是從活動代碼與格式按鈕
<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:gravity="center"
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.tabdemo.Tab1Activity"
android:background="#00547d"
>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Login"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
<Button
android:id="@+id/button2"
android:enabled="false"
android:text="Logout"
android:textColor="#FFFFFF"
android:textSize="20dp"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
/>
</LinearLayout>
</RelativeLayout>
有什麼使用方式款Android:背景= 「@繪製/ buttonshape」編程
你要設置按鈕的背景編程? – tahsinRupam
上面的java代碼創建了一個標準按鈕,但所有其他按鈕都是使用Buttonshape.xml的圖像。我想要我編程創建的按鈕也可以使用Buttonshape.xml。 –