Android布局,如何使两个项目在一行



我是安卓系统的初学者。我无法理解布局之间的差异。我想做一个按钮,在按钮旁边我想设置一个图像。那么我应该使用哪种布局,以及如何设置位置。(程序)

您需要访问以下页面:

通用布局对象

您需要使用LinearLayout-在线性布局中,您可以放置按钮和图像。

这里有一个关于如何使用LinearLayout的好教程:Android开发者LinearLayout

如果你是安卓系统的新手,我建议你看看那里的其他"Hello*"教程。

Cemal希望看到这是用程序完成的。上面的参考资料非常适合显示XML版本。这里是一个快速的例子,按钮和图像的线性布局完全由程序完成。

package com.example.android.ProgramLinearActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ProgramLinearActivity extends Activity {
   private static final int HORIZONTAL = 0;
/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      LinearLayout linearLayout = new LinearLayout(this);
      linearLayout.setOrientation(HORIZONTAL); //HORIZONTAL is default but here for clarity
      ImageView imageView = new ImageView(this);
      imageView.setImageResource( R.drawable.icon);
      Button button = new Button (this);
      button.setText("Test");
      linearLayout.addView(button);
      linearLayout.addView(imageView);
      setContentView(linearLayout);
   }
}

在eclipse编辑器中大量点击ctrl+space,可以查看有关按钮和imageview假发的其他属性的教程。

最新更新