2016-09-15 67 views
0

我为人人“的最小宽度”,我使用的按钮大小的倍数dimens.xml文件。而且我也有多个版本的主版面和按钮版面(用于多屏支持)。添加按钮的LinearLayout程序不能正常工作

我想插入多个按钮进入布局编程,并与XML布局夸大这个按钮。

这是怎么做到这一点:

Button temp_button = null; 
    final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    for(int i = 0; i < 7; i++) { 
     temp_button = (Button)inflater.inflate(R.layout.answer_letter_button, null); 
     temp_button.setTextColor(accent_color); 

     answer_letters.add(temp_button); 

     answer_panel.addView(answer_letters.get(i)); 
} 

这是这些按钮父布局:

<LinearLayout 
     android:id="@+id/answer_panel" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:gravity="center" 
     app:layout_constraintBottom_toTopOf="@+id/letter_panel"> 
</LinearLayout> 

和按键布局:

<Button 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="@dimen/answer_letter_button_size" 
    android:layout_height="@dimen/answer_letter_button_size" 
    android:text="M" 
    android:textStyle="bold" 
    android:textColor="@color/colorAccent" 
    android:textSize="@dimen/answer_letter_button_text_size" 
    android:layout_margin="@dimen/answer_letter_button_margin_size" 
    android:background="@drawable/shaped_button" /> 

我想看到什么: enter image description here

我居然看到:

enter image description here

我在做什么错了,有人告诉我?

对不起,我错误地表达。 我的意思是,结果按钮的大小有问题。大部分都是一个按钮的宽度。

+0

为什么要添加的按钮视图** answer_letters内​​部**第一,然后** ** answer_panel到? –

+0

你正在这里运行一个循环,这就是为什么按钮显示在那里。 –

回答

1

您必须添加layout_weight PARAM并将其设置为1也改变layout_widthlayout_heightwrap_content,你can'y从梦诗增加7项,并设置高度和宽度。你的方式总是错误的,结果也是。从维度中只设置textSize,并且看起来是你要搜索的内容。

<Button 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:text="M" 
     android:textStyle="bold" 
     android:textColor="@color/colorAccent" 
     android:textSize="@dimen/answer_letter_button_text_size" 
     android:layout_margin="@dimen/answer_letter_button_margin_size" 
     android:background="@drawable/shaped_button" /> 

并添加在您的父布局android:weightSum="7"

+0

对不起,我错误地表示。我的意思是,结果按钮的大小有问题。大部分都是一个按钮的宽度。 –

+0

立即试用此代码。 –

+0

谢谢!有用。 –