【问题标题】:create a 16:9 view创建 16:9 视图
【发布时间】:2013-01-18 21:11:33
【问题描述】:

我正在编写一个程序,我希望始终拥有 16:9 的屏幕。我设置了一个基本的线性布局来托管我编写的 2 个对象。第一个对象覆盖了 onMeasure 方法,因此它会从屏幕上占据一个“正方形”空间,而第二个对象会占据其余部分。这在我拥有的 16:9 设备上看起来不错。但是当我在其他设备上尝试时,它看起来很糟糕。我试图从承载我的对象的线性布局扩展,并覆盖布局的 onMeasure 方法。自定义 Linearlayout 似乎可以做到 16:9,但我的第一个对象(正方形)仍然是大正方形,而不是限制在 16:9 条带上的较小正方形。以下是相关代码

主要活动: @覆盖 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

    layout = new MyLinear(this);

    mField = new Field(this);
    mField.setId(1);

    control = new Controller(this);
    control.setId(2);

    layout.addView(mField);

    layout.addView(control);
    registerForContextMenu(control);

    setContentView(layout);
    mField.requestFocus();

}

自定义线性布局的 onMeasure 代码如下所示

public class MyLinear extends LinearLayout{

private int height, width; // dimension of the screen
private Context m_context;

public MyLinear(Context context) {
    super(context);
    m_context=context;
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    int height_temp = View.MeasureSpec.getSize(heightMeasureSpec);
    int width_temp = View.MeasureSpec.getSize(widthMeasureSpec);
    double ratio = width_temp/(double)height_temp;
    int final_height = (int) (width_temp/1.77);
    height=final_height;

    if (ratio<1.7) {
      setMeasuredDimension(width_temp, (int) (width_temp/1.77));

    } else {
      setMeasuredDimension(width_temp, height_temp);
    }

}

谁有更好的建议?

【问题讨论】:

  • 是的,我建议您花点时间接受您之前提出的一些答案,如果它们有帮助的话。

标签: android android-layout


【解决方案1】:

您编辑 view.xml 文件。添加 res/layout-xlarge/my_layout.xml 文件夹。添加 my_layout.xml 文件 res/layout-xlarge/ 文件夹后...对不起英语不好

【讨论】:

    猜你喜欢
    • 2019-03-19
    • 2012-05-08
    • 2020-04-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-06
    • 2015-06-29
    • 2013-11-13
    • 1970-01-01
    相关资源
    最近更新 更多