Android:如何为两种设备(平板电脑和手机)调用不同的布局。

huangapple go评论61阅读模式
英文:

Android: How to call a different layout for 2 devices (tablet and phone)

问题

我有一个应用程序,我想在手机和平板电脑上运行。我有两个不同的布局(横向排列),它们基本上是相同的,只是尺寸不同。

我的问题是,如何使MainActivity.java检测我正在运行的设备,并根据使用的设备触发活动。

我已经研究了片段,但我实际上不知道如何创建它们,更不用说如何使用它们了。

这是我的MainActivity.java:

public class MainActivity extends AppCompatActivity {

    EditText name;
    ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
    Intent intent;
    FirebaseDatabase rootNode;
    DatabaseReference reference;

    public void displayScore() {
        intent = new Intent(getApplicationContext(), Score.class);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE); //将隐藏标题
        getSupportActionBar().hide(); // 隐藏标题栏
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN); //启用全屏
        setContentView(R.layout.activity_main);

        final FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
        name = findViewById(R.id.editTextTextPersonName);
        oneStar = findViewById(R.id.imageView1);
        twoStar = findViewById(R.id.imageView2);
        threeStar = findViewById(R.id.imageView3);
        fourStar = findViewById(R.id.imageView4);
        fiveStar = findViewById(R.id.imageView5);
        oneStar.setTag(1);
        twoStar.setTag(2);
        threeStar.setTag(3);
        fourStar.setTag(4);
        fiveStar.setTag(5);

        class OnClickListener implements View.OnClickListener {

            @Override
            public void onClick(View view) {
                Bundle bundle = new Bundle();
                bundle.putString(FirebaseAnalytics.Param.VALUE, view.getTag().toString());
                if (name.length() == 0){
                    name.setError("请输入您的全名。");
                }
                else {
                    name.setError(null);
                    rootNode = FirebaseDatabase.getInstance();
                    reference = rootNode.getReference().child("Users");
                    //获取所有值
                    String username = name.getText().toString();
                    String value = view.getTag().toString();
                    int rating = Integer.parseInt(value);
                    UserHelper helper = new UserHelper(username,rating);
                    reference.push().setValue(helper);
                    Toast.makeText(MainActivity.this, "反馈成功提交!", Toast.LENGTH_SHORT).show();
                    sleep(2500);
                    displayScore();
                }
            }
        }
        oneStar.setOnClickListener(new OnClickListener());
        twoStar.setOnClickListener(new OnClickListener());
        threeStar.setOnClickListener(new OnClickListener());
        fourStar.setOnClickListener(new OnClickListener());
        fiveStar.setOnClickListener(new OnClickListener());
    }
}

任何帮助将不胜感激。

提前致谢。

英文:

I have an app that I want to run on both a phone and a tablet. I have 2 separate layouts (landscape) for both which are essentially the same except for their dimensions.

My question is how do I make the MainActivity.java detect the device that I'm running on and accordingly fire the activity based on the device being used.

I have looked into fragments but I couldn't really understand how to create them nevermind how to use them.

This is my MainActivity.java:

public class MainActivity extends AppCompatActivity {
EditText name;
ImageView oneStar, twoStar, threeStar, fourStar, fiveStar;
Intent intent;
FirebaseDatabase rootNode;
DatabaseReference reference;
public void displayScore() {
intent = new Intent(getApplicationContext(), Score.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //will hide the title
getSupportActionBar().hide(); // hide the title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); //enable full screen
setContentView(R.layout.activity_main);
final FirebaseAnalytics mFirebaseAnalytics = FirebaseAnalytics.getInstance(this);
name =  findViewById(R.id.editTextTextPersonName);
oneStar =  findViewById(R.id.imageView1);
twoStar =  findViewById(R.id.imageView2);
threeStar = findViewById(R.id.imageView3);
fourStar =  findViewById(R.id.imageView4);
fiveStar =  findViewById(R.id.imageView5);
oneStar.setTag(1);
twoStar.setTag(2);
threeStar.setTag(3);
fourStar.setTag(4);
fiveStar.setTag(5);
class OnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
Bundle bundle = new Bundle();
bundle.putString(FirebaseAnalytics.Param.VALUE, view.getTag().toString());
if (name.length() == 0){
name.setError("Please enter your full name.");
}
else {
name.setError(null);
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference().child("Users");
//Fetch all values
String username = name.getText().toString();
String value = view.getTag().toString();
int rating = Integer.parseInt(value);
UserHelper helper = new UserHelper(username,rating);
reference.push().setValue(helper);
Toast.makeText(MainActivity.this, "Feedback submitted successfully!", Toast.LENGTH_SHORT).show();
sleep(2500);
displayScore();
}
}
}
oneStar.setOnClickListener(new  OnClickListener ());
twoStar.setOnClickListener(new  OnClickListener ());
threeStar.setOnClickListener(new  OnClickListener ());
fourStar.setOnClickListener(new  OnClickListener ());
fiveStar.setOnClickListener(new  OnClickListener ());
}
}  

Any help would be appreciated.

Thanks in advance.

答案1

得分: 1

不应该针对平板电脑和手机查看两种不同的活动。为此,存在不同的资源布局文件夹。您在layout-land中创建横向布局,而纵向布局只需放在普通的布局文件夹中。然后,您只需创建一个活动,操作系统会根据屏幕和方向选择要使用的布局。

英文:

You should not be looking at two different activities for tablet and phone. There are different resource layout folders for this exact purpose. You create a layout for landscape in layout-land and your portrait is just in the normal layout folder. Then you have just one activity and the OS chooses the layout to use based on the screen and orientation

答案2

得分: 0

你的布局应该具有灵活性,以适应不同的尺寸。你应该只有一个布局适用于所有尺寸,避免硬编码的尺寸。

这是来自https://developer.android.com/training/multiscreen/screensizes的摘录。

"避免硬编码的布局尺寸。为了确保你的布局具有灵活性,并适应不同的屏幕尺寸,你应该在大多数视图组件的宽度和高度上使用"wrap_content"和"match_parent",而不是硬编码的尺寸。
"wrap_content"告诉视图将其大小设置为适应视图内的内容所需的大小。
"match_parent"使视图在父视图内尽可能扩展。"

英文:

Your layout should be flexible to adapt to different sizes. You should have only a single layout for all sizes, avoid hardcoded sizes.
This is an excerpt from https://developer.android.com/training/multiscreen/screensizes

"Avoid hard-coded layout sizes. To ensure that your layout is flexible and adapts to different screen sizes, you should use "wrap_content" and "match_parent" for the width and height of most view components, instead of hard-coded sizes. 
"wrap_content" tells the view to set its size to whatever is necessary to fit the content within that view.
"match_parent" makes the view expand to as much as possible within the parent view."

huangapple
  • 本文由 发表于 2020年7月22日 03:41:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63021952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定