㈠ android用代碼怎麼設置layout
代碼中可以布局layout,有專門的layout類,你查下資料吧,
不過還是建議盡量在xml中布局layout,代碼中會增加程序運行負擔,請採納謝謝
㈡ 新建Android項目layout什麼都沒有
你在向導里頭選了「empty project」之類的選項,所以不幫你生成layout。其實自己生成也很簡單,右鍵單擊layout,選擇new,選擇Android Layout File,選擇FrameLayout或者LinearLayout,就可以生成一個。然後new一個class,繼承自Activity,在OnCreate里頭調用setContentView(R.layout.XXX);就行了,其中XXX是你剛才建立的layout的文件名。
㈢ android中點擊哪個是把類和layout一起創建
一般創建project,開發環境會自動給mainActivity創建一個layout!但是之後自己創建類的時候,開發環境就不會自動再創建了!
不過,這個有插件可以實現,可以搜索一下,Android studio的常用插件試一試!
㈣ Android中,怎麼通過代碼設置layout背景
Android中view 通過代碼設置 layout首先確定要設置的layout是哪種layuot,這里以LinearLayout為例,首先步驟如下:1、首先在代碼中創建一個LinearLayout.LayoutParams對象,然後設置其寬高代碼如下:LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(20,30);2、然後設置margin、padding之類的屬性,如下:3、最後設置給一個控制項,如下:private TextView mTextView;mTextView = (TextView) findViewById(R.id.text);mTextView.setLayoutParams(ll);
㈤ android studio 怎麼在layout中創建文件夾
layout創建方式:
在res/layout目錄,右鍵 new XML 選擇layout即可
然後給layout起個名字,注意文件名只能是小寫,不能有大寫和中文
㈥ Android中view 怎樣通過代碼設置 layout
Android中view 通過代碼設置 layout首先確定要設置的layout是哪種layuot,這里以LinearLayout為例,首先步驟如下:
1、首先在代碼中創建一個LinearLayout.LayoutParams對象,然後設置其寬高代碼如下:
java">LinearLayout.LayoutParamsll=newLinearLayout.LayoutParams(20,30);
㈦ Android自定義layout怎麼寫
LinearLayout自定義方法有多種:
1、自定義xml布局,然後載入布局,自定義一個View繼承LinearLayout
2、在自定義控制項中聲明它的所有子元素,然後在Layout文件中像使用LinearLayout一樣去進行布局。
第二種比較煩 ,它需要在Layout文件中定義好子元素之後,要在代碼 onFinishInflate() 進行匹配子元素。
我就說說載入布局文件的方法吧。
首先:定義好layout文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="40dip"
android:paddingTop="5dip"
android:src="@drawable/right_icon" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="8dip"
android:text="主題"
android:textColor="#000000" />
<LinearLayout
android:layout_width="100dp"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/home_icon" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingBottom="5dip"
android:paddingLeft="12dip"
android:paddingTop="5dip"
android:src="@drawable/add_icon" />
</LinearLayout>
</LinearLayout>
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<cn.com.demo.view.MyLinearLayout
android:id="@+id/ll_actionbar"
android:layout_height="fill_parent<span style="font-family: Tahoma, 'Microsoft Yahei', Simsun;">" </span>
android:layout_width="wrap_content"
android:background="@drawable/bg"
/>
</LinearLayout>
接下來自定義一個MyLinearLayout繼承LinearLayout,並且載入剛剛寫好的layout文件。(比如http://www.tiecou.com)
public class MyLinearLayout extends LinearLayout {
private ImageView imageView,iv_home,iv_add;
private TextView textView;
public MyLinearLayout (Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MyLinearLayout (Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.actionbar, this);
imageView=(ImageView) findViewById(R.id.imageView1);
iv_home=(ImageView) findViewById(R.id.imageView2);
iv_add=(ImageView) findViewById(R.id.imageView3);
textView=(TextView)findViewById(R.id.textView1);
}
/**
* 設置圖片資源
*/
public void setImageResource(int resId) {
imageView.setImageResource(resId);
}
/**
* 設置顯示的文字
*/
public void setTextViewText(String text) {
textView.setText(text);
}
}
最後,要的時候使用定義好的MyLinearLayout控制項。
㈧ android studio 新建項目 layout中為什麼有兩個xml文件
android studio 新建項目 layout中為有兩個xml文件是因為創建項目選擇的布局文件問題,有些布局文件創建出來有兩個甚至更多,要創建項目中只有Layout布局中只有一個XML文件,只需要在選擇布局的時候選擇Empty Activity即可,操作步驟如下:
1、雙擊打開Android studio之後選擇start a new Android Studio project,如下圖: