Android 用源代码写layout布局

通过纯代码实现XML同样的效果:

[java]   view plain copy
  1. import android.app.Activity;  
  2. import android.content.Context;  
  3. import android.graphics.Color;  
  4. import android.os.Bundle;  
  5. import android.text.InputFilter;  
  6. import android.text.InputFilter.LengthFilter;  
  7. import android.view.Gravity;  
  8. import android.view.ViewGroup;  
  9. import android.view.ViewGroup.LayoutParams;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12. import android.widget.ImageView;  
  13. import android.widget.ImageView.ScaleType;  
  14. import android.widget.LinearLayout;  
  15. import android.widget.RelativeLayout;  
  16. import android.widget.ScrollView;  
  17. import android.widget.TextView;  
  18.   
  19. public class ActivityInfo extends Activity {  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25. //      setContentView(R.layout.info);  
  26.           
  27.         initUI();  
  28.     }  
  29.       
  30.     public final void initUI(){  
  31.         ScrollView main = new ScrollView(this);  
  32.         main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
  33.         main.setBackgroundColor(Color.WHITE);  
  34.           
  35.         //根布局参数  
  36.         LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);  
  37.         layoutParamsRoot.gravity = Gravity.CENTER;  
  38.         //根布局  
  39.         LinearLayout layoutRoot = new LinearLayout(this);  
  40.         layoutRoot.setLayoutParams(layoutParamsRoot);  
  41.         layoutRoot.setOrientation(LinearLayout.VERTICAL);  
  42.           
  43.           
  44.         //上边距(dp值)  
  45.         int topMargin = dip2px(this30);  
  46.         //imageMain宽度(dp值)  
  47.         int widthMain = dip2px(this240);  
  48.         //imageMain高度(dp值)  
  49.         int heightMain = dip2px(this120);  
  50.           
  51.         //imageMain布局参数  
  52.         LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(widthMain,heightMain);  
  53.         layoutParamsImageMain.topMargin = topMargin;  
  54.         layoutParamsImageMain.bottomMargin = topMargin;  
  55.         layoutParamsImageMain.leftMargin = topMargin;  
  56.         layoutParamsImageMain.rightMargin = topMargin;  
  57.         layoutParamsImageMain.gravity=Gravity.CENTER_HORIZONTAL;  
  58.         //初始化ImageView  
  59.         ImageView imageMain = new ImageView(this);  
  60.         imageMain.setScaleType(ScaleType.FIT_CENTER);  
  61.         imageMain.setAdjustViewBounds(true);  
  62.         imageMain.setBackgroundColor(Color.BLACK);  
  63.         imageMain.setImageResource(android.R.drawable.ic_dialog_map);  
  64.         layoutRoot.addView(imageMain, layoutParamsImageMain);  
  65.           
  66.         //textInfo布局参数  
  67.         LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);  
  68.         layoutParamsTextInfo.topMargin = topMargin;  
  69.         layoutParamsTextInfo.bottomMargin = topMargin;  
  70.         layoutParamsTextInfo.leftMargin = topMargin;  
  71.         layoutParamsTextInfo.rightMargin = topMargin;  
  72.         layoutParamsTextInfo.gravity=Gravity.CENTER_HORIZONTAL;  
  73.         //初始化textInfo  
  74.         TextView textInfo = new TextView(this);  
  75.         textInfo.setGravity(Gravity.CENTER_HORIZONTAL);  
  76.         textInfo.setTextSize(18);  
  77.         layoutRoot.addView(textInfo, layoutParamsTextInfo);  
  78.           
  79.         //editInfo布局参数  
  80.         LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(widthMain,LayoutParams.WRAP_CONTENT);  
  81.         layoutParamsEditInfo.topMargin = topMargin;  
  82.         layoutParamsEditInfo.gravity=Gravity.CENTER_HORIZONTAL;  
  83.         //初始化editInfo  
  84.         EditText editInfo = new EditText(this);  
  85.         editInfo.setHint("请输入文字内容");  
  86.         //设置可输入的最大长度  
  87.         InputFilter[] filters = {new LengthFilter(200)};    
  88.         editInfo.setFilters(filters);  
  89.         editInfo.setTextSize(18);  
  90.         layoutRoot.addView(editInfo, layoutParamsEditInfo);  
  91.           
  92.         //上边距(dp值)  
  93.         int minHeight = dip2px(this54);  
  94.         //上padding(dp值)  
  95.         int topPadding = dip2px(this4);  
  96.         //左padding(dp值)  
  97.         int leftPadding = dip2px(this2);  
  98.         //按钮布局  
  99.         LinearLayout layoutButton = new LinearLayout(this);  
  100.         layoutButton.setLayoutParams(layoutParamsEditInfo);  
  101.         layoutButton.setOrientation(LinearLayout.HORIZONTAL);  
  102.         layoutButton.setBackgroundColor(Color.parseColor("#c6c3c6"));  
  103.         layoutButton.setMinimumHeight(minHeight);  
  104.         layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding);  
  105.         layoutButton.setId(100000001);  
  106.           
  107.         //buttonOK布局参数  
  108.         LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  109.         layoutParamsButtonOK.gravity = Gravity.LEFT;  
  110.         layoutParamsButtonOK.leftMargin = dip2px(this10);  
  111.         layoutParamsButtonOK.rightMargin = dip2px(this5);  
  112.         layoutParamsButtonOK.weight = 1;  
  113.         //Button确定  
  114.         Button buttonOK = new Button(this);  
  115.         buttonOK.setLayoutParams(layoutParamsButtonOK);  
  116.         buttonOK.setMaxLines(2);  
  117.         buttonOK.setTextSize(18);  
  118.         buttonOK.setText("确定");  
  119.         layoutButton.addView(buttonOK);  
  120.           
  121.         //buttonCancel布局参数  
  122.         LinearLayout.LayoutParams layoutParamsButtonCancel = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
  123.         layoutParamsButtonCancel.gravity = Gravity.RIGHT;  
  124.         layoutParamsButtonCancel.leftMargin = dip2px(this5);  
  125.         layoutParamsButtonCancel.rightMargin = dip2px(this10);  
  126.         layoutParamsButtonCancel.weight = 1;  
  127.         //Button取消  
  128.         Button buttonCancel = new Button(this);  
  129.         buttonCancel.setLayoutParams(layoutParamsButtonCancel);  
  130.         buttonCancel.setMaxLines(2);  
  131.         buttonCancel.setTextSize(18);  
  132.         buttonCancel.setText("取消");  
  133.           
  134.         layoutButton.addView(buttonCancel);  
  135.           
  136.         layoutRoot.addView(layoutButton, layoutParamsEditInfo);  
  137.           
  138.         //RelativeLayout布局参数  
  139.         LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);  
  140.         RelativeLayout layoutBottom = new RelativeLayout(this);  
  141.         layoutBottom.setLayoutParams(layoutParamsBottom);  
  142.           
  143.         RelativeLayout.LayoutParams paramsImageBottom = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  144.         paramsImageBottom.addRule(RelativeLayout.BELOW, 100000001);  
  145.         paramsImageBottom.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);  
  146.         paramsImageBottom.setMargins(topMargin, topMargin, topMargin, topMargin);  
  147.           
  148.         //初始化ImageView  
  149.         ImageView imageBottom = new ImageView(this);  
  150.         imageBottom.setScaleType(ScaleType.FIT_CENTER);  
  151.         imageBottom.setAdjustViewBounds(true);  
  152.         imageBottom.setBackgroundColor(0xFF777777);  
  153.         imageBottom.setImageResource(android.R.drawable.ic_dialog_email);  
  154.         layoutBottom.addView(imageBottom, paramsImageBottom);  
  155.         layoutRoot.addView(layoutBottom);  
  156.           
  157.           
  158.         //TODO TEST  
  159. //      imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map);  
  160.         textInfo.setText("测试文本显示");  
  161.           
  162.         main.addView(layoutRoot);  
  163.         setContentView(main);  
  164.     }  
  165.       
  166.     /** 
  167.      * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 
  168.      */  
  169.     public static int dip2px(Context context, float dpValue) {  
  170.         final float scale = context.getResources().getDisplayMetrics().density;  
  171.         return (int) (dpValue * scale + 0.5f);  
  172.     }  
  173.   
  174.     /** 
  175.      * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 
  176.      */  
  177.     public static int px2dip(Context context, float pxValue) {  
  178.         final float scale = context.getResources().getDisplayMetrics().density;  
  179.         return (int) (pxValue / scale + 0.5f);  
  180.     }  
  181.   
  182. }  

 

效果图:


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android 翻牌源代码是一个非常常见的开发需求,它通常用于展示卡片或图片,并提供一种翻转效果,给用户带来更好的交互体验。以下是一个可运行的Android翻牌源代码示例: 首先,在XML布局文件中,我们创建一个包括两个ImageView的LinearLayout,其中一个ImageView用于显示背面图像,另一个ImageView用于显示正面图像。然后,设置背面图像为默认图像。 ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/image_back" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/default_image" /> <ImageView android:id="@+id/image_front" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/default_image" /> </LinearLayout> ``` 然后,在Java代码中,我们需要实现点击屏幕翻牌的逻辑。我们可以使用Animation和AnimatorSet两个类来实现动画效果。当点击时,我们将正面图像向左旋转90度,并将背面图像向左旋转90度并设置可见。完成旋转后,我们交换正面图像和背面图像。 ```java public class FlipCardActivity extends Activity { private boolean isFrontVisible = true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_flip_card); final ImageView imageBack = findViewById(R.id.image_back); final ImageView imageFront = findViewById(R.id.image_front); imageBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // 创建正面图像的旋转动画 ObjectAnimator animFront = ObjectAnimator.ofFloat(imageFront, "rotationY", 0f, 90f); animFront.setDuration(500); // 设置动画持续时间 // 创建背面图像的旋转动画 ObjectAnimator animBack = ObjectAnimator.ofFloat(imageBack, "rotationY", -90f, 0f); animBack.setStartDelay(250); // 设置动画延迟时间 animBack.setDuration(500); // 设置动画持续时间 animBack.addListener(new Animator.AnimatorListener() { @Override public void onAnimationStart(Animator animation) {} @Override public void onAnimationEnd(Animator animation) { if (isFrontVisible) { imageFront.setVisibility(View.GONE); imageBack.setVisibility(View.VISIBLE); } else { imageBack.setVisibility(View.GONE); imageFront.setVisibility(View.VISIBLE); } isFrontVisible = !isFrontVisible; } @Override public void onAnimationCancel(Animator animation) {} @Override public void onAnimationRepeat(Animator animation) {} }); // 创建动画集合 AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playSequentially(animFront, animBack); animatorSet.start(); } }); } } ``` 通过以上代码,我们可以实现一个简单的Android翻牌效果。可以根据实际需求,定制图像或动画细节等。这只是一个基本示例,你可以在该示例的基础上进一步扩展和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值