更改高度按钮android代码中的java代码

问题描述:

我想在片段中创建方形按钮。我的片段布局文件是:更改高度按钮android代码中的java代码

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


<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:padding="5dp" 
    android:weightSum="2" 
    > 
    <Button 
     android:id="@+id/main_menu_news" 
     android:text="@string/main_menu_news_text" 
     style="@style/buttons" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_marginRight="5dp" 
     /> 

    <Button 
     android:id="@+id/main_menu_insta" 
     android:text="@string/main_menu_insta_text" 
     style="@style/buttons" 
     android:layout_width="0dp" 
     android:layout_weight="1" 
     android:layout_marginLeft="5dp" 
     /> 
</LinearLayout> 

我改变按钮的高度onCreateView。这一功能的实现是:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
    super.onCreateView(inflater, container, savedInstanceState); 
    View view=inflater.inflate(R.layout.main_menu, container, false); 
    ViewGroup layout = (ViewGroup) getActivity().getLayoutInflater().inflate(R.layout.main_menu, null); 
    for(int i=0;i<layout.getChildCount();i++){ 
     View v=layout.getChildAt(i); 
     if(v instanceof ViewGroup){ 
      ViewGroup vg=(ViewGroup)v; 
      for(int j=0;j<vg.getChildCount();j++){ 
       View v2=vg.getChildAt(j); 
       if(v2 instanceof Button){ 
        Button btn=(Button)v2; 
        btn.setHeight(btn.getMeasuredWidth()); 
       } 
      } 
     } 
    } 
    return view; 
} 

我不能改变按钮的高度onCreateViewgetMeasuredWidth函数总是返回0 如何解决呢?

+0

是有任何规范从代码设置高度的目的?我想如果你从xml设置它的性能会更好 – temnoi

+0

看看这个http://*.com/questions/26715942/change-the-size-of-a-fragment-with-a-button –

+0

可以我在xml中创建方形按钮?我想按钮的宽度是屏幕宽度的一半。 @temnoi – hamid

要解决这一点,你可以创建一个包裹内容层面的线性布局,并添加该按钮,该布局。然后设置按钮的高度,我想的话,getMeasuredWidth()不应该返回0

这是我的一段代码,你可以根据你的

LinearLayout newLL = new LinearLayout(mContext); 
newLL.setLayoutParams(newLinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 
newLL.setOrientation(LinearLayout.HORIZONTAL); 
newLL.setGravity(Gravity.LEFT); 
newLL.addView(btn); 
widthSoFar = btn.getMeasuredWidth(); 
+0

你能举个例子吗? – hamid

+0

我编辑了我的答案。希望它能帮助你。 –

+0

谢谢。工作。 – hamid

button.setLayoutParams(new LinearLayout.LayoutParams(10, 100)); 

Example here.

您可以从XML

做定制

enter image description here

<?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="wrap_content" 
android:orientation="horizontal"> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@color/colorAccent" 
    android:gravity="center"> 

    <Button 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:text="btn1" /> 
</LinearLayout> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:background="@color/colorPrimary" 
    android:gravity="center"> 

    <Button 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:text="btn2" /> 
</LinearLayout>