按钮对纵向和横向执行不同的操作

  • 本文关键字:操作 执行 横向 按钮 android
  • 更新时间 :
  • 英文 :


在纵向模式下,我有一个带有三个按钮的应用程序。当用户点击第一个按钮时,文本 #1 将出现,如果用户点击第二个按钮,文本 #1 消失,文本 #2 出现在同一个位置,与按钮三的想法相同。我正在尝试让文本视图仅在横向模式下显示每个按钮的图像。请问我可以这样做吗?请参阅下面的我的代码。谢谢!

.XML:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 1"
    android:id="@+id/click_btn"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/response"
    android:layout_below="@+id/click_btn"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/button15"
    android:layout_alignEnd="@+id/button15" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 2"
    android:id="@+id/button15"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Recipe 3"
    android:id="@+id/button16"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />
<fragment
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:name="android.webkit.WebViewFragment"
    android:id="@+id/fragment"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true" />

爪哇岛:

package com.example.android;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import static com.example.android.R.*;

public class Recipes extends ActionBarActivity implements View.OnClickListener {
TextView resp;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(layout.activity_recipes);
    resp = (TextView)this.findViewById(id.response);
    Button b = (Button)this.findViewById(id.click_btn);
    Button c = (Button)this.findViewById(id.button15);
    Button d = (Button)this.findViewById(id.button16);
    b.setOnClickListener(this);
    c.setOnClickListener(this);
    d.setOnClickListener(this);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_recipes, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
    switch(v.getId()){
        case R.id.click_btn: /** on click_btn button click */
            resp.setText("2   medium sweet potatoes (about 1 pound total) n1/2  teaspoon saltn1/2  teaspoon ground cuminn1/2  teaspoon chili powdern1/2  teaspoon paprikan1/4  teaspoon ground black pepper");
            break;
        case R.id.button15: /** on button15 button click */
            resp.setText("1 large egg yolk, at room temperaturen1/2 cup extra-virgin olive oil; more for grillingn2 teaspoons minced fresh flat-leaf parsleyn1 teaspoon minced fresh tarragonn1 1/2 teaspoons fresh lemon juice; more to taste Kosher salt");
            break;
        case R.id.button16: /** on button16 button click */
            resp.setText("1/2 cup butter, softenedn1 cup packed light brown sugarn1 1/2 cups all-purpose flourn3 cups rolled oatsn1 teaspoon ground cinnamon");
            break;
    }
}
}

我认为您需要在ImageView中使用不同的横向布局。

您可以在 res. 的布局土地文件夹中创建横向布局。如果没有布局土地文件夹,则创建一个。

在布局-土地文件夹中创建与默认布局同名的新横向布局。

您必须在 java 代码中检查空指针,因为在默认模式下没有图像视图。

 if (getResources().getConfiguration().orientation == 2)

检查您是否在 Java 中处于横向模式,但如果我理解正确,您可能希望将整个活动设置为固定景观。在您的清单中执行此操作:

<activity
    ...
    android:screenOrientation="landscape" />

最新更新