【问题标题】:How to set linear layout background in Android如何在Android中设置线性布局背景
【发布时间】:2016-02-24 09:08:24
【问题描述】:

我对 Android 很陌生。如何将背景图像设置为整个我的相对布局,我的图像来自服务器?请告诉我如何更改背景。

这是我的代码,每张图片都通过 JSON。

ActivityClass.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie_rev_fulldis_activity);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //movie title namename text view
    movie_title=(TextView)findViewById(R.id.revi_fulldes_movietitle);

    //movie image in reviews
    movie_image=(ImageView)findViewById(R.id.revi_fulldes_movieImage);

    mov_pos=Integer.parseInt((getIntent().getExtras()).getString("mov_pos"));

    movie_title.setText(Reviews_update.revData.get(mov_pos).getNewsTitle());

    Picasso.with(getApplicationContext()).load((Reviews_update.revData.get(mov_pos)).getNewsImage()).into(movie_image);
}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/revi_main_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="20dp"
    android:weightSum="1">

    <LinearLayout
        android:layout_marginTop="110dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FFFFFF">

        <FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:layout_width="120dp"
                android:layout_height="160dp"
                android:id="@+id/revi_fulldes_movieImage"
                android:layout_weight="0"
                android:padding="10dp"/>

        </FrameLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:layout_weight="3">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/revi_fulldes_movietitle"
                android:textSize="18dp"
                android:padding="10dp"
                android:textStyle="bold"
                android:text="Moviewheewd  ewudwd wedewd w"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/revi_fulldes_movierev_sitename"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="20dp"
                android:text="simple  jwehd wj dewjd"/>

        </LinearLayout>

    </LinearLayout>

【问题讨论】:

  • 你可以使用一些图片加载库,比如通用图片加载来从url设置图片
  • 他已经在用毕加索了
  • 你能展示使用布局xml文件吗?
  • 您需要下载并在布局中显示图像吗?
  • 我正在使用 picaso lib 进行提货@Sree

标签: android android-linearlayout


【解决方案1】:

使用毕加索的回调

Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){

@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
 mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}

@Override
public void onBitmapFailed(final Drawable errorDrawable) {
  Log.d("TAG", "FAILED");
}

@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
  Log.d("TAG", "Prepare Load");
}      
})

为了更好地理解,请参阅this

【讨论】:

    【解决方案2】:

    像这样替换你的毕加索代码,你会实现你想要的, 试试看!

    Picasso.with(this)
            .load(url)
            .into(new Target() {
                @Override
                public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
                    /* Save the bitmap or do something with it here */
                    BitmapDrawable background = new BitmapDrawable(bitmap);
                    linearLayout.setBackgroundDrawable(background);
                }
        });
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多