#java #android
Вопрос:
Если у меня есть горизонтальное представление, созданное в xml.
Есть ли какой-нибудь способ создать горизонтальное прокручиваемое представление в этом представлении ?
Заранее спасибо.
Комментарии:
1. вам нужно показать нам, что вы пробовали до сих пор ! это не так работает !
Ответ №1:
Вы можете использовать HorizontalScrollView
https://developer.android.com/reference/android/widget/HorizontalScrollView
Образец
MainActivity.java
package com.dinkar.dummyjava;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HorizontalScrollView horizontalScrollView = new HorizontalScrollView(this);
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
horizontalScrollView.setLayoutParams(layoutParams);
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
linearLayout.setLayoutParams(linearParams);
horizontalScrollView.addView(linearLayout);
ImageView imageView1 = new ImageView(this);
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params1.setMargins(30, 20, 30, 0);
params1.gravity = Gravity.CENTER;
imageView1.setLayoutParams(params1);
imageView1.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView1);
ImageView imageView2 = new ImageView(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params2.setMargins(0, 20, 30, 0);
params2.gravity = Gravity.CENTER;
imageView2.setLayoutParams(params2);
imageView2.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView2);
ImageView imageView3 = new ImageView(this);
LinearLayout.LayoutParams params3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params3.setMargins(0, 20, 30, 0);
params3.gravity = Gravity.CENTER;
imageView3.setLayoutParams(params3);
imageView3.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView3);
ImageView imageView4 = new ImageView(this);
LinearLayout.LayoutParams params4 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params4.setMargins(0, 20, 30, 0);
params4.gravity = Gravity.CENTER;
imageView4.setLayoutParams(params4);
imageView4.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView4);
ImageView imageView5 = new ImageView(this);
LinearLayout.LayoutParams params5 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params5.setMargins(0, 20, 30, 0);
params5.gravity = Gravity.CENTER;
imageView5.setLayoutParams(params5);
imageView5.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView5);
ImageView imageView6 = new ImageView(this);
LinearLayout.LayoutParams params6 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params6.setMargins(0, 20, 30, 0);
params6.gravity = Gravity.CENTER;
imageView6.setLayoutParams(params6);
imageView6.setImageResource(R.drawable.ic_launcher_background);
linearLayout.addView(imageView6);
FrameLayout frameLayout = findViewById(R.id.container);
if (frameLayout != null) {
frameLayout.addView(horizontalScrollView);
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@ id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Комментарии:
1. Я имею в виду, как я могу сделать это программно?
2. не могли бы вы, пожалуйста, принять ответ и проголосовать, если это вам помогло.