Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android如何使布局的背景图像响应?_Android_User Interface_Layout - Fatal编程技术网

Android如何使布局的背景图像响应?

Android如何使布局的背景图像响应?,android,user-interface,layout,Android,User Interface,Layout,我不熟悉安卓系统 我正在创建一个速度表应用程序,我想把速度表的特定图像作为数字文本的背景。我已经附上了我为预览而编写的代码 我已经附上了所需界面的图像和xml预览的图像及其代码 如何使布局更具响应性 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas

我不熟悉安卓系统

我正在创建一个速度表应用程序,我想把速度表的特定图像作为数字文本的背景。我已经附上了我为预览而编写的代码

我已经附上了所需界面的图像和xml预览的图像及其代码

如何使布局更具响应性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:background="@color/background"
tools:context=".fragment.Tab2">

<!-- TODO: Update blank fragment layout -->
<ScrollView
    android:id="@+id/digitalView"
    android:layout_width="wrap_content"
    android:layout_centerInParent="true"

    android:scrollbars="none"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="vertical"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_text"
        android:gravity="center"
        >
        <TextView
            android:layout_width="wrap_content"
            android:id="@+id/speedTextDigital"
            android:textColor="@android:color/white"
            android:text="00"
            android:maxLength="3"
            android:textSize="90sp"
            android:gravity="center"
            android:layout_height="wrap_content"

            />

        <TextView

            android:id="@+id/unit"
            android:layout_marginTop="0dp"
            android:layout_gravity="center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="KM/H"
            android:textSize="30sp"
            android:textColor="@android:color/white"/>
    </LinearLayout>

    </ScrollView>

</RelativeLayout>

我想制作的UI:

我现在得到的用户界面:


如果要使背景图像可单击,需要执行以下步骤:

1.将id添加到视图中,假设您正在使用ImageView:

 <ImageView
    android:layout_width="match_parent"
    android:id="@+id/image_to_click"
    android:layout_height="match_parent"
    android:src="@drawable/xxx"
    android:scaleType="centerCrop"
    />
3.现在您必须实现其方法:

 @Override
public void onClick(View view) {      
    switch (view.getId()) {

      case R.id.image_to_click:  /*this is id from xml file (layout)*/
          /*put action you want to get after clicking on image*/
          break; 
     } 
  }
4.最后,在活动的onCreate方法中添加这两个方法:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imageView=findViewById(R.id.image_to_click);
      advancedButton.setOnClickListener(this); }

这将完成这项工作,这不是处理单击视图的唯一方法,但在大多数情况下,该模式是相似的

发布您的代码和您想要实现的内容请尽可能发布您的布局xml以及所需和实际屏幕。我发布了代码、原始图像和所需图像。我认为您的方法不适合您的需要,恐怕您应该实现自定义视图,以便在速度箭头和仪表板内进行适当的计算,否则,您将陷入不同屏幕比例的问题。一个起点可以是这个存储库:我已经完成了速度的模拟视图。这个gui实际上是数字视图。。
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      imageView=findViewById(R.id.image_to_click);
      advancedButton.setOnClickListener(this); }