2016-12-22 51 views
0

我有一个关于android布局的问题。我无法实现我正在寻找的东西,我需要一些方向。 我正在寻找将在滚动视图布局内的布局。它很难解释,但一张图片胜过千言万语。 enter image description hereAndroid的滚动视图和布局问题

我想这是一个滚动视图中,可以在任何帮助吗?

+0

你想在滚动视图内的图像视图?或者,滚动视图中的整个布局? –

回答

0

如果你不想动态添加图像比这是你想要试试这个布局。

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ScrollView 
     android:id="@+id/sv" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <LinearLayout 
      android:id="@+id/linMain" 
      android:layout_width="match_parent" 
      android:layout_height="fill_parent" 
      android:orientation="horizontal"> 

      <RelativeLayout 
       android:id="@+id/lin1" 
       android:layout_width="100dp" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp"> 

       <ImageView 
        android:id="@+id/imgView1" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:background="@android:color/holo_purple" /> 

       <ImageView 
        android:id="@+id/imgView2" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView1" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_red_light" /> 

       <ImageView 
        android:id="@+id/imgView3" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView2" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_green_light" /> 

       <ImageView 
        android:id="@+id/imgView4" 
        android:layout_width="100dp" 
        android:layout_height="100dp" 
        android:layout_below="@id/imgView3" 
        android:layout_marginTop="20dp" 
        android:background="@android:color/holo_blue_light" /> 
      </RelativeLayout> 

      <LinearLayout 
       android:id="@+id/lin2" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_margin="10dp" 
       android:orientation="vertical"> 

       <WebView 
        android:id="@+id/webView" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:layout_toRightOf="@id/linImages"></WebView> 

      </LinearLayout> 


     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 

这里我附上此布局UI截图。

enter image description here

+0

工作!真棒! – sohail

0

试试这个,我已经activity_main包含GridView和网页流量由两个布局..

1)。

2)activity_gridview其中包含imageview将在gridview中膨胀。

我已经使用BaseAdapter将图像动态添加到Gridview。

在MainActivity中,我已经将Adapter设置为GridView。试试这个代码一次。

activity_main.xml中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="fill_parent" 
     android:layout_height="match_parent" 
     android:fillViewport="false" 
     android:orientation="vertical"> 

     <LinearLayout 
      android:id="@+id/linImages" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal"> 

      <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/grid" 
       android:layout_width="100dp" 
       android:layout_height="match_parent" 
       android:numColumns="1"></GridView> 

      <WebView 
       android:id="@+id/webView" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_toRightOf="@id/linImages"></WebView> 
     </LinearLayout> 


    </ScrollView> 

</RelativeLayout> 

activity_gridview.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <ImageView 
     android:id="@+id/imgView1" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star2" /> 

    <ImageView 
     android:id="@+id/imgView2" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star1" /> 

    <ImageView 
     android:id="@+id/imgView3" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star3" /> 

    <ImageView 
     android:id="@+id/imgView4" 
     android:layout_width="100dp" 
     android:layout_height="100dp" 
     android:background="@drawable/star1" /> 

</LinearLayout> 

CustomAdapter.java:

public class CustomAdapter extends BaseAdapter { 
    Context context; 
    int flags[]; 
    LayoutInflater inflter; 

    public CustomAdapter(Context applicationContext, int[] flags) { 
     this.context = applicationContext; 
     this.flags = flags; 
     inflter = (LayoutInflater.from(applicationContext)); 
    } 

    @Override 
    public int getCount() { 
     return flags.length; 
    } 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

    @Override 
    public long getItemId(int i) { 
     return 0; 
    } 

    @Override 
    public View getView(int i, View view, ViewGroup viewGroup) { 
     view = inflter.inflate(R.layout.activity_gridview, null); 
     ImageView imgView1 = (ImageView) view.findViewById(R.id.imgView1); 
     ImageView imgView2 = (ImageView) view.findViewById(R.id.imgView2); 
     ImageView imgView3 = (ImageView) view.findViewById(R.id.imgView3); 
     ImageView imgView4 = (ImageView) view.findViewById(R.id.imgView4); 

     imgView1.setImageResource(flags[0]); 
     imgView2.setImageResource(flags[1]); 
     imgView3.setImageResource(flags[2]); 
     imgView4.setImageResource(flags[3]); 
     return view; 
    } 
} 

MainActivity.java:

public class MainActivity extends AppCompatActivity { 

    int images[] = {R.drawable.star2, R.drawable.star1, R.drawable.star3, R.drawable.star1}; 
    GridView grid; 
    CustomAdapter customAdapter; 
    WebView webView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     webView= (WebView) findViewById(R.id.webView); 
     grid = (GridView) findViewById(R.id.grid); 
     customAdapter = new CustomAdapter(getApplicationContext(), images); 
     grid.setAdapter(customAdapter); 
    } 
}