Android 是否可以从活动文件中为LinearLayout设置背景?

Android 是否可以从活动文件中为LinearLayout设置背景?,android,android-layout,Android,Android Layout,这是我的xml代码: <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"> 嗯,线性布局也有setter setBackground

这是我的xml代码:

<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">

嗯,线性布局也有setter

setBackgroundColor()

继承自视图类

Hmm还有用于LinearLayout的setter

setBackgroundColor()
是从视图类继承的是,您可以通过

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="+@id/linear_layout_id"
            android:width = "fill_parent"
            android:height = "fill_parent"
            android:orientation = "vertical"
    />
是的,你可以自己做

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="+@id/linear_layout_id"
            android:width = "fill_parent"
            android:height = "fill_parent"
            android:orientation = "vertical"
    />

就像你为按钮做的一样,你也可以为线性布局做同样的事情。为您提供Linearlayout id并设置颜色:-

((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);

就像你为按钮做的一样,你也可以为线性布局做同样的事情。为您提供Linearlayout id并设置颜色:-

((LinearLayout)findViewById(R.id.ll_test)).setBackgroundColor(Color.BLUE);

但是,如何在活动类中获取LiniarLayout对象?与示例中获取ImageButton的方法相同。我想你知道的。请看ADR答案。但我如何在活动类中获取LiniarLayout对象?与示例中获取ImageButton的方法相同。我想你知道的。看看答案。