2017-04-22 75 views
0

我有一个滚动视图按钮的列表,但是当我通过编程添加一些其他按钮的风格是不同的,我不知道为什么。 正如我们在下一张图片中看到的,添加的按钮更加黑暗,当我点击按钮时,按钮的背景变成蓝色。 但我的默认按钮是深灰色。为什么按钮样式改变时,我编程添加?

有捕获:

enter image description here

按钮 “6” 是XML按钮来添加。另一个是以编程方式添加的。

有我的XML:

<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/diese"> 


    <LinearLayout 
        android:orientation="vertical" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
     <ScrollView 
        android:id="@+id/scrollView" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_marginBottom="50dp"> 

     <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/darker_gray" > 

      <TableLayout 
        android:id="@+id/tableLayout1" 
        android:layout_width="match_parent" 
        android:layout_height="700dp" > 

       <TableRow 
         android:layout_height="0dp" 
         android:layout_weight="1" > 

        <Button 
          android:id="@+id/one" 
          android:layout_width="0dp" 
          android:layout_height="fill_parent" 
          android:layout_weight="1" 
          android:gravity="center" 
          android:text="1" /> 
       </TableRow> 

有我的代码添加按钮:

TableLayout SV =(TableLayout)findViewById(R.id.tableLayout1);

TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,2200,500); 

    sv.setLayoutParams(layoutParams); 

    View view = findViewById(R.id.diese); 

    Diese d1 = new Diese(1,"test"); 
    d1.addDieseToXml(sv,this); 
    Diese d2 = new Diese(2,"Coucou"); 
    d2.addDieseToXml(sv,this); 

Thx很多。上下文“this”引用我的ButtonActivity。

+0

也许按钮.setActivated(真);或者出于某种原因,我更喜欢gridView以编程方式创建按钮 – ORY

回答

0

你应该向我们展示你的风格文件和Diese类?我觉得这是一个风格问题。

但是,鉴于提供的信息,我几乎可以确定“addDieseToXml”或Diese类本身定义了按钮样式,并且不会将该样式应用于您的xml按钮(如您所说,深灰色是颜色你自找的)。

因此,我建议你到一个风格应用到您的按钮,

style="@style/YourStyle" 

直接在XML按钮的代码。

其他选项是通过改变你的主题,以这种风格应用到所有的活动按钮(如果你想的话),加入

<item name="android:buttonStyle">@style/YourStyle</item> 
+0

我没有为按钮定义样式...我找不到线索我的问题>< – Valt

+0

您为应用程序定义了基本主题吗? – Feuby

+0

不,我没有什么特别的,当我在xml中添加一个按钮。当他们被点击时,他们是灰色和灰色的,当我通过编程添加他们时,他们是灰色的,但是当我点击时他们是蓝色的 – Valt

相关问题