2012-11-10 33 views
0

这是我的XML代码:是否可以从Activity文件为LinearLayout设置背景?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:background="@color/blue" 
android:gravity="center"> 

可以从我的活动文件中设置的颜色?

例如,我可以做到这一点对一个按钮:

ImageButton x = (ImageButton) this.findViewById(R.id.btn1); 
x.setBackgroundColor(color); 

回答

1

是继承,你可以做它由

public class MainActivity { 
    private LinearLayout ll; 

    @Override 
    public void onCreate(...) { 
     super.onCreate(...); 
     setContentView(R.layout.your_layout_name); 
     ll = (LinearLayout) findViewById(R.id.linear_layout_id); 

     // You can set Background Color for your Linear Layout 
     ll.setBackgroundColor(Color.RED); 
     // You can set Image Also 
     ll.setBackgroundResource(R.drawable.imagename); 
} 
} 

在XML文件中:

<LinearLayout 
      android:id="[email protected]/linear_layout_id" 
      android:width = "fill_parent" 
      android:height = "fill_parent" 
      android:orientation = "vertical" 
    /> 
1

嗯还有制定者的LinearLayout

setBackgroundColor() 

从View类

+0

但是我怎样才能获得我的Activity类中的LiniarLayout对象? – Andreea

+0

与您在示例中获得ImageButton的方式相同。我以为你知道这一点。看看ADR的答案。 – Rad

1

就像你这样做是为了可以为在LinearLayout中做同样也是按钮。给你Linearlayout一个ID并设置颜色: -

((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE); 
相关问题