
正文
用java代码写布局 java中常用的布局有哪些
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
Android Studio如果在java中编写布局,代码放在哪个文件中?什么位置?
1.使用代码编写一个底部选项卡用java代码写布局的布局
2.整个页面用java代码写布局的容器布局(包含Fargment,分割线用java代码写布局,选项卡)
private void initView(Context context) {
setBackgroundColor(0xfff6f6f6);
FrameLayout frameLayout=new FrameLayout(context);//选项界面容器
frameLayout.setId(FL_ID);
View lineView=new View(context);//分割线
lineView.setId(LINE_ID);
RelativeLayout.LayoutParams rlParams=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
rlParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlParams.addRule(RelativeLayout.ABOVE , LINE_ID);
lineView.setBackgroundColor(lineColor);
RelativeLayout.LayoutParams rlParams2=new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, DensityUtils.dip2px(context, 1));
rlParams2.addRule(RelativeLayout.ABOVE , TAB_ID);
addView(frameLayout, rlParams);//选项界面容器
addView(lineView,rlParams2);//分割线
//选项卡容器
linearLayout=new LinearLayout(context);
linearLayout.setBackgroundColor(tabBgColor);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setGravity(Gravity.CENTER_VERTICAL);
linearLayout.setId(TAB_ID);
tabNum=tabIcos.length;
for (int i = 0; i tabNum; i++) {
View view = createIndicator(tabIcos[i], tabtxts[i], tabItemTvColor, "itemTag"+i, "icoTag" + i, "txtTag" + i);
view.setOnClickListener(OnClick);
if(i== nowTabIndex){//初始化选项卡
changeTab(view, i);
}
linearLay
相关问答
Q1: java图形界面的布局
没用过SWT的人路过用java代码写布局,提供一个SWING:
窗口用JFrame(frame)用java代码写布局,布局为BorderLayout.
顶部为JLabel(labelTop),带图片和文字,用frame.add(labelTop, BorderLayout.NORTH)加入
左面为JPanel(panelLeft),布局为BoxLayout,参数Y_AXIS.需要在创建JPanel时设置Layout为NULL,然后再创建BoxLayout时需要把已经建立的panelLeft作为参数传进去,然后再设置panelLeft的Layout为那个BoxLayout。用frame.add(panelLeft, BorderLayout.WEST)加入
中间为一堆不同功能的JPanel,布局根据功能而定了,在点击按钮时,移除frame此时中间的panel(记录在一个自己的变量panelCenter里),然后加入按钮对应的panel,再把加入的panel存入变量panelCenter.
Q2: 怎么用java代码写一个线性布局;布局里面有两个按钮是水平的
android 使两个按钮水平排列的方法是使用lineLayout线性布局,如下代码:
?xml version="1.0" encoding="utf-8"?
LinearLayout xmlns:android=""
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="vertical"
View
android:layout_width="wrap_content"
android:layout_height="1.2px"
android:layout_marginBottom="7dp"
android:background="@color/white" /
LinearLayout
android:layout_width="fill_parent"
android:layout_height="79dp"
android:layout_weight="2"
android:orientation="horizontal"
android:layout_margin="10dp"
Button
android:id="@+id/bt1"
android:layout_width="fill_parent"
android:layout_height="26dp"
android:background="@drawable/shape"
android:layout_weight="1"
android:text="确认对冲"
android:textColor="@color/white"
android:textSize="15dp" /
Button
android:layout_width="fill_parent"
android:layout_height="26dp"
android:background="@drawable/shapeyuanjiao"
android:layout_weight="1"
android:text="取消"
android:textColor="@color/white"
android:textSize="15dp" /
/LinearLayout
/LinearLayout
运行结果如下:
关于用java代码写布局和java中常用的布局有哪些的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。








