Rosicky
V2EX  ›  Android

安卓开发,不使用 layout_weight、程序动态计算,怎么在布局文件中画一条在各种手机屏幕上占 1/2 屏幕宽度的线?

  •  
  •   Rosicky · Feb 24, 2016 · 13791 views
    This topic created in 3745 days ago, the information mentioned may be changed or developed.
    12 replies    2016-02-24 15:29:02 +08:00
    kifile
        1
    kifile  
       Feb 24, 2016
    记得有一个 PercentLayout ,但是其实也是控件帮你封装了百分比计算。
    iluhcm
        2
    iluhcm  
       Feb 24, 2016
    FrameLayout, android:gravity=centerInHoricental
    814084764
        3
    814084764  
       Feb 24, 2016
    不用尺,不用测量,怎么画出一条 1 米的线?
    em70
        4
    em70  
       Feb 24, 2016 via iPhone
    @814084764 靠运气徒手画,练个几十年可能略有小成
    missingbobo
        5
    missingbobo  
       Feb 24, 2016
    这不是自己找事吗
    Strikeactor
        6
    Strikeactor  
       Feb 24, 2016
    画三条,两边的调透明
    twoyuan
        7
    twoyuan  
       Feb 24, 2016
    「不使用程序动态计算」
    这个要么自己算,要么系统(类库)封装好了对应的属性、方法帮你算,不然……把屏幕从中间对折? :)
    a0000
        8
    a0000  
       Feb 24, 2016 via Android
    2 天路被你堵死了,难不成得用意念控制啊
    Bown
        9
    Bown  
       Feb 24, 2016
    相对布局,中心放一个不占宽高的空 view ,画线 view 方边缘和它之间
    gccdchen
        10
    gccdchen  
       Feb 24, 2016
    在满屏的 `FrameLayout` 里, 添加 一个 View
    `background = "@color/black" layout_gravity="center"`
    karnaugh
        11
    karnaugh  
       Feb 24, 2016
    给你个思路吧.
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
    double width, height;
    height = width = getMeasuredWidth();
    widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) width, MeasureSpec.EXACTLY);
    heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    这个段代码我是用在自定义 LinearLayout 里的.效果是这个 LinearLayout 是一个正方形.边长为原宽度.你自己改改就出来咯,不过我觉得还是 weight 好使..
    zhgg0
        12
    zhgg0  
       Feb 24, 2016
    LinearLayout xxxLine = (LinearLayout) findViewById(R.id.xxx_line);
    xxxLine.getLayoutParams().width = 屏幕宽带 / 2;
    xxxLine.requestLayout();

    xxx_line 写在 xml 里,高度在 xml 里写,宽度随便写,然后用代码重新设置下宽带就好了。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3264 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 109ms · UTC 11:29 · PVG 19:29 · LAX 04:29 · JFK 07:29
    ♥ Do have faith in what you're doing.