动态添加7个按钮,以便在Android中调整所有屏幕尺寸,使其达到宽度和高度

时间:2016-07-27 04:29:38

标签: android android-layout button

我想根据不同的屏幕尺寸动态添加按钮。我怎么能得到它。

这是我尝试过的。但当我将第一个按钮从WRAP_CONTENT更改为静态高度时,屏幕上不会显示任何按钮

public void setButtons(){
    // button one params
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
    params.setMargins(380,433,10,20);

    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params1.setMargins(229,-45,50,20);

    LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params2.setMargins(239,60,10,20);

    LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params3.setMargins(422,-45,10,20);

    LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params4.setMargins(592,-455,10,20);

    LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(getMyNewX(76),getMyNewX(76));
    params5.setMargins(592,65,10,20);

    //   LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(76,76);
    //     params6.setMargins(592,-275,10,20);

    LinearLayout layout = new LinearLayout(getApplicationContext());

    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(params);
    Button button1 = new Button(getApplicationContext());
    button1.setLayoutParams(params);
    // button1.setText("like");
    button1.setBackgroundDrawable(getResources().getDrawable(R.drawable.like));
    // button1.setBackgroundColor(Color.TRANSPARENT);

    Button button2 = new Button(getApplicationContext());
    button2.setLayoutParams(params1);
    //    button2.setText("share");
    button2.setBackgroundDrawable(getResources().getDrawable(R.drawable.share));
    //      button2.setBackgroundColor(Color.TRANSPARENT);

    Button button3 = new Button(getApplicationContext());
    button3.setLayoutParams(params2);
    //    button3.setText("mute");
    button3.setBackgroundDrawable(getResources().getDrawable(R.drawable.mute));
    //      button3.setBackgroundColor(Color.TRANSPARENT);

    Button button4 = new Button(getApplicationContext());
    button4.setLayoutParams(params3);
 button4.setBackgroundDrawable(getResources().getDrawable(R.drawable.download));


    Button button5 = new Button(getApplicationContext());
    button5.setLayoutParams(params4);
    button5.setBackgroundColor(Color.TRANSPARENT);


    Button button6 = new Button(getApplicationContext());
    button6.setLayoutParams(params5);
    button6.setBackgroundColor(Color.TRANSPARENT);


    layout.addView(button1);
    layout.addView(button2);
    layout.addView(button3);
    layout.addView(button4);
    layout.addView(button5);
    layout.addView(button6);

    buttonLayout.addView(layout);

}

 originalWidth = 1100;
    originalHeight = 2050;

    DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
    width = displayMetrics.widthPixels;
    height = displayMetrics.heightPixels;


}


 int getMyNewX(int originalX)
{
    //originalX : x for line segment
    return width/originalWidth * originalX;
}
int getMyNewY(int originalY)
{
    //originalY : y for line segment
    return height/originalHeight * originalY;
}

4 个答案:

答案 0 :(得分:0)

您可以通过这种方式动态添加重量

var inMemList = new List<string>();

foreach (ObjectId attId in attCol)
{
    AttributeReference attRef = (AttributeReference)tr.GetObject(attId, OpenMode.ForWrite);
    inMemList.Add(attRef.TextString);
}


foreach(var text in inMemList.Distinct())
    ed.WriteMessage(string.Format("\n {0}", text));

答案 1 :(得分:0)

在Android工作室

res -> values -> dimens.xml 

enter image description here

在dimens.xml文件

中声明维度低于w820dp的设备

enter image description here

维度(w820dp)文件中的设备维度w820dp及更高版本

enter image description here

然后在您的代码中使用它,如

enter image description here

Android会自动为不同的分辨率选择所需的尺寸

**w820dp - for screens with more than 820dp of available width. This
         would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively).**

this may also help you

答案 2 :(得分:0)

有两种解决方案。

解决方案1 ​​:将dp转换为像素

private int convertPxToDp(int px){
return Math.round(px/(Resources.getSystem().getDisplayMetrics().xdpi /DisplayMetrics.DENSITY_DEFAULT));
}

现在,您拥有所有屏幕尺寸的动态像素值。

解决方案2 :为所有按钮添加重量

如果是垂直方向,那么

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LayoutParams.WRAP_CONTENT, 0);

否则

LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( 0, LayoutParams.WRAP_CONTENT);

然后

params.weight = 1.0f;
Button button = new Button(this);
button.setLayoutParams(params);

答案 3 :(得分:0)

您需要使用具有权重属性的layoutparams来平等地固定宽度。找到下面的代码。

.config(function(IdleProvider) {
        IdleProvider.idle(5);
        IdleProvider.timeout(5);
    })
    .run(['Idle', function(Idle){
        // start watching when the app runs. also starts the Keepalive service by default.
        Idle.watch();
    }])
    .controller('ctrl', function($scope) {
        $scope.$on('IdleTimeout', function() {
            let time = new Date();
            console.log('idle timeout at ', time);
        });

        $scope.$on('IdleStart', function() {
            let time = new Date();
            console.log('idle start at ', time);
        });
    })